Sei sulla pagina 1di 3

My Interview consisted of two rounds : These are questions asked: 1.

Given a pointer to a node in a binary search tree, find the inorder successor of that node (the node that will be visited next after this in an inorder trave rsal). Assume that every node has a pointer to the parent(root->parent=-1)? Ans: Asked me to write the code for it. t is the pointer tree* inorder(tree *t) { If t->right!=NULL search leftmost node in the right subtree. else current = t; while(current->parent!=-1) { if(current==current->parent->left) return current->parent; current=current->parent; } return NULL; } I started of with wrong code ; he hinted the presence of bugs and finally write the above one.

2. Given numbers from 1 to n, find the number of combinations of r numbers such that no two numbers in a combination are consecutive. Find the recursive relatio n and Write a program to do the same given a number n and r. How do u minimize s pace in this case? Ans: recursive relation : F(n,r)= F(n-2,r-1) + F(n-1, r) Program: I said using Dynamic programming - n x r matrix (array) Compute values row after row , he suggested column-wise computation. Space minimization : I couldn't come up with it first ,

then he suggested the example of fibonnaci number computation. I said I said minimum required is n but he said it 2*n (I was wrong :) ).

3. Given a sentence , how do you reverse the order of words in it ? Ans: Reverse the entire array (sentence) and then individual words.

4. How do find rectangular intersection? Ans: Bentley -ottmann algorithm . He asked me to explain it.

5. How do you find median of numbers given in two sorted arrays of size n. Compare the elements at n/2 in each array if equal : it is the median. if not equal : recursive call for median of the half of array right to the small er one and half of the array left to the larger of the two.

6. Given 1000 (distributed)systems and 1 million numbers , how do you find the m edian of those numbers? Every system has a set of numbers and they are not sorte d. Ans: Similar to the 'Select' algorithm pick a pivot from one of them and send that pivot to other systems. They'll retu rn the number of elements to the right and left of the pivot . Accordingly , call the select depending on the number of elements less than (or greator than) pivot .

7. Given a polygon and a point how do you find whether it lies inside the polygo n ? Ray - Scan algorithm : odd number of intersection - inside ; even - outside

8. SKyline problem. Given a set of intersecting rectangles (whose sides are para llel to the axes and bottom edge rests on the x-axis), how do you find the upper envelope of them. Sort the horizontal edges (top edges of the rectangles) into an event queue and insert them into a max heap based on their y- coordinates. Any changes to the ro ot of the heap is stored(skyline) and delete the edges when u encounter the othe r end of the segment.

9. How do you determine whether two regular expressions generate same language ? Compute the minimal DFA for the expressions and compare the corresponding states and the transitions from them.

Potrebbero piacerti anche