Beruflich Dokumente
Kultur Dokumente
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.
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.