Sei sulla pagina 1di 6

Hi Friends ( For 70 + Companies Model papers , Best Interview tips, Resume tips & Interview question on .

NET, Testing tools, J2EE & many more Visit www.sureshkumar.net Check out all sections U will get very usefull stuff ) PL'z'e Don't replay to this email ID Any Questions or Suggestions Mail to Admin@sureshkumar.net If u want to help other ppl send any usefull stuff ( papers , Interview questions n tutorials send them to papers@sureshkumar.net I wish u all the Best ) Best & TOP c++ Interview questions.... Q1: Tell how to check whether a linked list is circular. A: Create two pointers, each set to the start of the list. Update each as follows: while (pointer1) { pointer1 = pointer1->next; pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next; if (pointer1 == pointer2) { print (\"circular\n\"); } } Q2: OK, why does this work? If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, its either 1 or 2 jumps until they meet. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Why is it difficult to store linked list in an array? How can you find the nodes with repetetive data in a linked list? Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable but aac or bcd are unacceptable. This is a C question that I had for an intern position at Microsoft: Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique. After I wrote out my function, he asked me to figure out from the code how many times the printf statement is run, and also questions on optimizing my algorithm. 1. i think this will work for while(node) { If(node==start) printf(circular); node=node->next; } 2. How about those codes: node = start->next; while(node)

{ if(node == start) { printf(circular); return; } node = node->next; } 3. The posted solution is flawed in that it can (and usually will) seg-fault on non-circular lists. A correct solution should have a not circular result as well. 4. how can u find no of elements in an array quickly? if it is a dynamic array(linked list) the tradition is to keep the count variable in the head and incrementing it everytime an insertion is performed and decrementing whenever a deletion is performed.so by seeing that count vaiable we can easily tell no. of elements. 5. Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable but aac or bcd are unacceptable. program ************************************************** #define true 1 #define false 0 int* isalreadyread; /* this array is used to make sure that int main(void) the characters in the original,which r matched with a char { in the dup is marked as already read*/ char* original; /* this string holds the original string like..abc*/ char* dup; /* this string is used to hold the duplicates like acb bac cab cba*/ int isokay(char* ,char* ); /* this is the function(which intern calls many) which checks whether the string is okay*/ int origlength; int flag; void initilaizing_isalreadyread(); printf(\n enter the original string); scanf(%s,original); origlength=strlen(original); isalreadyread=(int*) calloc(origlength,sizeof(int)); flag=true; while(flag) { printf(enter the duplicate string);scanf(%s,dup); initializing_isread(origlength); flag=isokay(original,dup); if(flag) printf(%s,dup); } } /* end of main*/ int isokay(char* original,char* dup) { int reflength=strlen(original);

int duplength=strlen(dup); int found(char c,char* str); if(reflength!=duplength) return false; /* this is obvious*/ for(int i=0;i 6. Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. It can be observed that the number of combinations of string, of length N, is N!. Consider an example: string: 123 Length: 3 Combination: 3! = 6 Output: 123 132 321 312 231 213 Following is a program to print all the combination a string: 1 /* 2 * str_combi.c - Print all the combinations of a string 3 * Author - Vijay Kumar R Zanvar 4 * Date - Feb 16, 2004 5 */ 6 7 #include 8 #include 9 #include 10 11 void 12 string_combi ( char * s, int len ) 13 { 14 int i; 15 char tmp; 16 static int j; 17 static char *p; 18 19 if ( !p ) 20 p = s; 21 22 for ( i = 1; i 2 ) 25 string_combi ( s+1, len-1 ); 26 else 27 { 28 j++; 29 printf ( %d: %s\t, j, p ); 30 if ( !( j%10 ) ) 31 puts ( ); 32 } 33 if ( i

7. Got a question that extends on the detecting circular linked list question: Given the constraints that you cannot modify the struct (or class) of the elements, and that you have to remove the loop in the cyclic linked list in O(n) time and O(1) space, how would you do it? By removing the loop, I mean something like:

_______________________________ __ __ __ _| __ __ __ _| | | -> | | -> | | -> | | -> | | -> | | -> | | -> | | Given this linked list, how do you make the last element in the list to point to NULL in O(n) time and O(1) space? I couldnt think of a solution that uses O(1) space without modifying the structure. If I was allowed, I could just have a visited flag inside the struct. Ill then have 2 pointers, a current and a previous. For every node traversed by the current, I will set the flag to true, and each traversal, I will check whether that flag is set to true. If it is, then the previous pointer is the last element of the list, and I can just set the next to NULL. This will also be in O(n) time. Any insights on this? 8. The picture of the linked list was kinda screwed up Anyway, that picture has 8 elements, with the last element pointing to the 4th element, thus making a loop in the list. 9. answer for finding that link list is circular is int count=0; while (node) { if(node == start && count != 0) { printf(Circular); exit(0); } p = node->next; q = node->next; if(node == start) node->next = null; 10. answer for finding that link list is circular is int count=0; while (node) { if(node == start && count != 0) { printf(Circular); exit(0); } p = node->next; q = node->next; if(node == start) node->next = null; else node->next = r; p->next = node; node = q; r = p; count = count + 3; } 11. iscircular(Node* head) /* check whether a link list is circular */ { /* assume that linklist has a header which keeps a count of no. of nodes in the list */

Node* temp=head; while(head->data) temp=temp->next; if(temp->next=head->next)/*1st node actually is next node to head*/ return TRUE; return FALSE; } 12. constructor can be static or not?and why? in c++

13. Not to mention the glaring access-violation once pointer2 becomes NULL(though this error indicates not circular), the unnecessity of traversing pointer1(though doing so does not impact numof iterations), and the lack of terminating conditions(though still technically within the guidlines by only checking for circular), the solution is FLAWED in that it will print Circular for non-circular lists with 1 node!!! (when Pointer1 and Pointer2 are both NULL) I did think it was a kinda cool idea to weed out the noncircular lists in (num of Nodes)/2 iterations, but the extra traversal code i think outweighs any benefit. My elegant solution, if i do say so myself: //assert: list is not empty for(node=head->next; node!=NULL && node!=head; node=node->next); if(node==NULL) cout 14. oops, that last statement should read: if(node==NULL) printf(Non-circular); else printf(Circular); 15. dawid, i dont think my code is lack of terminating condition. i have put while(head->data) but i should have copied that head->data into a temporary variable. it should look like int temp1=head->data while(temp1) i dont think it will print circular for all single node lists. if thenext of the single node is the address of the nodeit will print circular. otherwise it will print not circular. the idea is to get the address in the next field of the last node (node which was inserted latest) i dont think there is a better way than traversing to reach last node. Andin ur code u assumed that the last nodes next point to the Head. i think its not a good design. it ruins the whole list then. 16. Sorry karthikeyan, I should have been more clear: I was referring to the original solution (by default). It seems to have been torn up enough already, but i wanted to show off my fancy 1 line traversal. However, in rebuttal: -Youre fired! Mistaking an equality operator(==) with an assignment (=) is an offense punishable by death. -Your solution is unintelligible with that mangled loop condition. However relying on a count of num of nodes makes your algorithm possibly unscalable to lists with dummy nodes, for example.

-A chain of pointers will either end in a NULL pointer(if programmed responsibly), or youll eventually be back where you started again. I dont see how this testing ruins a list. Your point is noted, though, about a list with a dummy head that is bypassed around the circle (Like this: -0 ); but that doesnt look like a circle to me, and I feel that head should point to the first node and any extra information should be stored in a wrapping class (because starting at the front of the list would mean dereferencing twice; silly if traversed many times each game loop, for example). However, to address that issue for those small standalone-pointers-as-lists, you would send the first real node ( head->next ) to this function. It is not completely efficient in that there is no specific head to terminate at - you must traverse all the way back around to the given node, but it works for any list. My beautiful(concise) function, if i do say so myself: bool isCircular(Node* node1) { if(node1==NULL) return false; for(Node* node2=node1->next; node2!=NULL && node2!=node1; node2=node2->next); return (node2!=NULL); } Regards, Dawid. 17. dawid, ur code looks efficient 18. 19. David, your code does not work in case the loop does not begin with the first element. For example: A->B->C->D->C I agree though that the question was check that a linked list is circular and not check that a linked list contains a circle. 20. int main(int argc, char* argv[]) { //Write a prog to accept a given string in any order and flash error if any of //the character is different. For example : If abc is the input then abc, //bca, cba, cab bac are acceptable but aac or bcd are unacceptable. //program char code[100]; char ip[100]; int i,count,flag=0; puts(Enter the string:); scanf(%s,ip); puts(Enter the comparision string:); scanf(%s,code); for(i=0;i

Potrebbero piacerti anche