I mean is the solution valid? Because pushing will take one traversal and then popping another?
On Tue, Jul 5, 2011 at 4:58 PM, Dumanshu <[email protected]> wrote: > @Saurabh: u can improve upon ur solution. > > char * outstr; > int c =0; > void getreversedwords(string str) > { > index = str.length()-1; > //outstr is the answer > stack st; > while(index) > { > st.push(str[index]); > if(st.top() == ' ') //top is a space > st.printstack(); > index--; > } > if(st.isEmpty()==false) > st.printstack(); > } > > void printstack() > { > int set =1; > char ch; > if(st.top()==' ') > { > ch = st.pop(); > set =0; > } > while(st.isEmpty()==false) > outstr[c++] = st.pop(); > if(!set) > outstr[c++] = ch; > } > > On Jul 5, 4:10 pm, saurabh singh <[email protected]> wrote: > > well for question 6 I think calculating size of string will count as one > > traversal? > > Correct me if I am wrong? > > My approach: > > traverse storing each char in a string.when space encountered push the > > string in stack.... > > I am not sure how my solution is but it doesnt appears gud ryt now. > > > > > > > > > > > > On Tue, Jul 5, 2011 4:34 PM, Dumanshu <[email protected]> wrote: > > > ans1. use counting sort for character array (0 to 255) then check for > > > the second string if same or not. > > > > > ans2. send 1 and 2, 1 comes back, send 10 and 5, 2 comes back, send 2 > > > and 1 > > > > > ans3. As vikas said, sum of digits should b 8. In that case the number > > > must have a zero digit because 1st digit the no. of zeroes can't be > > > zero then. right? print all the combinations. need help on this. > > > > > ans4. a function with string, first index, last index as arguments and > > > return true or false. increment first index and decrement last index > > > for recursive call. > > > > > ans5. I have no idea. may b we can take help of shunting algorithm. > > > but there ought to be an easier way to do this. Do we have to WAP a > > > general one for these expressions??? > > > > > ans6. allocate same number of bytes for the reversed string. now > > > traverse the original string from the back. > > > as soon as u encounter a space say at position x or you come to 0 > > > index, call the current = function (x+1,current) or current = > > > function(0,current). > > > this function copies the original string from position x+1 to > > > (while(space or null)) in reversed string at position current and > > > returns current + no. of characters copied. > > > > > ans8. somebody plz explain this. do we have to find the values for > > > a,b,c,d,e,f so that we can get return change for 100,50,25,10,5? > > > 10, 5,5,20,25,50... how to get change for 5 then? > > > > > ans9. lets say we have to allocate 100*200 i.e. rows 100 and columns > > > 200 > > > int **a = (int **)malloc(sizeof(int *)*100); > > > a[0] = (int *)malloc(sizeof(int)*100*200); > > > for(i=1;i<100;i++) > > > a[i] = (a[i-1]+200); > > > > > ans10. use hashtables, first traversal get true for all values > > > present. then second traversal, check if (X- arr[i]) is present in > > > hashtable, if yes print. > > > > > ans11. i think ans to this one is 1 byte. because some information is > > > stored.. may be. m nt sure :P > > > > > ans12. typedef struct node * NODE; > > > > > NODE sortlist(NODE head,int n) > > > { > > > if(n==1) return head; > > > if(!n) return null; > > > NODE temp =head; > > > for(int i=0;i<n\2;i++)temp=temp->next; > > > return merge(sortlist(head,n/2),sortlist(temp,n/2 + (n%2));); > > > } > > > > > NODE merge(NODE t1,NODE t2) > > > { > > > NODE head=NULL,temp=NULL; > > > int set =0; > > > > > while(t1 && t2) > > > { > > > if(t1->data < t2->data) > > > { > > > if(!set){head=t1;temp = head;set=1;t1=t1->next;} > > > else > > > { > > > temp->next = t1; > > > t1=t1->next; > > > } > > > } > > > else > > > { > > > if(!set){head=t2;temp = head;set=1;t2=t2->next;} > > > else > > > { > > > temp->next = t2; > > > t2=t2->next; > > > } > > > } > > > > > while(t1) > > > { > > > temp->next = t1; > > > t1 = t1->next; > > > } > > > while(t2) > > > { > > > temp->next = t2; > > > t2 = t2->next; > > > } > > > temp->next = NULL; > > > > > return head; > > > } > > > > > did i miss any case? > > > > > On Jul 5, 11:24 am, vikas <[email protected]> wrote: > > > > These all were asked cumulatively in five interviews, one after > another. > > > > > > Q1 given 2 string A ,B. write a code to find all character of A > exists in > > > B > > > > or not? > > > > > > Q2. A puzzle athttp://mathforum.org/library/drmath/view/56756.html > > > > > > Q3. Find a 8-digit number, where the first figure defines the count > of > > > zeros > > > > in this number, the second figure the count of numeral 1 in this > number > > > and > > > > so on.... > > > > > > Q4. write a recursive function to find a given string is palindrome > or > > > not? > > > > > > Q5. write a code to generate the parse tree like compilers do > > > internally > > > > for any given expression > > > > e.g. a+(b+c*(e/f)+d)*g > > > > > > Q6. 3 reverse the string word by word e.g. "My name is pradeep" .. > o/p > > > shd > > > > be "pradeep is name > > > > My" ...intwer expecting a 1 traversal algo > > > > > > Q7. C++ (What are virtual functions) what happened if I do > > > > Base *d = new Derived(); and Derived *d = new Base(); > > > > is it error(in which statement) or not if yes what type of error? > > > > > > Q8. you have 6 coins of Indian denomination (a+b+c+d+e+f=115 paisa) > > > ...if > > > > user ask for a > > > > change of 100,50,25,10,5 paisa then you r not able to give find the > value > > > of > > > > a,b,c,d,e,f.........e.g. > > > > lets if user ask for a change of 25 paisa then you r not able to > return > > > > (10+10+5) or (20+5) > > > > > > Q9. allocate 2D array dynamically with min no. of malloc calls. > > > > > > Q10. given unsorted array and a no. X ,find 2 no. whose sum is > > > > X....a[i]+a[j]=X ...........do it in O(n) > > > > > > Q11. class A {}; A obj ; what is sizeof(obj)....explain ANS > > > > > > Q12. Write a code of Merge sort on Linked list. > > > > > > Lets discuss them................ > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Algorithm Geeks" group. > > > To post to this group, send email to [email protected]. > > > To unsubscribe from this group, send email to > > > [email protected]. > > > For more options, visit this group at > > >http://groups.google.com/group/algogeeks?hl=en. > > > > -- > > Saurabh Singh > > B.Tech (Computer Science) > > MNNIT ALLAHABAD- Hide quoted text - > > > > - Show quoted text - > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- Saurabh Singh B.Tech (Computer Science) MNNIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
