Google Interview Question for Software Engineer in Tests


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
2
of 4 vote

That would require O(n) space. Can this be done in constant space

- Anonymous October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

traverser one Tree and insert into the others?

- etlds October 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Yep. Just one caveat. Since this is inplace merge you will be resetting the children pointers hence only post order traversal will work there.

- Anonymous October 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

It's straight forward if two BSTs are converted to LL first. Sorted LL merging is done in-place.

- anonymous October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi, how to convert BST to LL without using extra space? As far as I understand, linked list should take extra space, O(n), where n is the number of nodes.

- xiaohan2012 February 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do inorder traversal for one tree and for each element traversed insert the same in the other one...

- KK October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

Doing post-order traversal will be good because you need to remove that node from that tree and add it to another one...so if you remove the children of the node first then it would be easy to remove the node itself later..
Any Comment????

- KK October 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@KK Hey buddy i think that should work. Nice one.. :)

- jackass October 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But it will take O(nlogn) in worst case it can go O(n^2)

- Anonymous May 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think you can treat this as the delete node algorithm of the BST where the node deleted is the root node.

- Garnie October 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Gamie
I did not get your soln.Can you pls elaborate

- learner October 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Will this algorithm work? Assume the two trees are T1 with root1 and T2 with root 2. Assume we are going to merge(insert) T2 into T1

Node Insert(root1,root2)
{
if(root2==null)
return ;

find_place_to_insert_in_T1(root2);
if(root2 is left child of parent)
{
Node temp=insert(root1,root2->right);
if(temp!=root2)
root2->right=Null;
}
else
Node temp=insert(root1,root2->right);
if(temp!=root2)
root2->right=Null;

- Ransack October 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Will this algorithm work? Assume the two trees are T1 with root1 and T2 with root 2. Assume we are going to merge(insert) T2 into T1. Sorry code was incomplete last time.

Node Insert(root1,root2)
{
if(root2==null)
return ;

find_place_to_insert_in_T1(root2);
if(root2 is left child of parent)
{
Node temp=insert(root1,root2->right);
if(temp!=root2)
root2->right=Null;
}
else
{
Node temp=insert(root1,root2->left);
if(temp!=root2)
root2->left=Null;
}
return parent of root2
}

Basically the code is trying to find a place to insert a node with its children into another tree. If the node is inserted as a left child, then all its left children preserve the BST property but the right children may violate it so inserting the right children again from the top. Vice versa if node is inserted as right child. The check is to reset the pointer. In case the right child is not assigned to the same place as before, we need to reset the pointer to null. An optimisation is to pass the current parent into the recursion. So in case the node is inserted into the same place, then one need not call the recursion for its children since it preserves the BST property inherently.

- Ransack October 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Lets say the two trees are
V1 V2
/ \ / \
L1 R1 L2 R2
where V1,V2 are the values of roots and L1,L2,R1,R2 are the left and right sub trees.

1)Check which of V1,V2 is bigger and make that the root of our final answer(lets say V2>V1)
2)Then we make it root of the final merged tree.
3)Now we can surely say that( V1 )and L2 will be on left of V2
( / )
( L1 )
4)therefore let X=Merge(V1_L1_tree , L2)
5)Now nodes in R1, can lie either to the left or right of V2
6)therefore let Y=Merge(V2_R2_tree , R1)
7)After this, we can safely merge the left subtree of Y with X and make the head of this result as the left child of root of Y.
8)Not we need to specifically see the fact that we said 3) with respect to V2 but when we were making Y, it might have happened that V2 was replaced from the head of Y(and may be 3) would not hold then) but that will happen only in case R1->value > V2->value (as the bigger value is made root as in 1) ) and still 3) will hold

The solution is from 1-7. 8 is just proof
Please mention if anybody finds any mistake.

- Hill Billy November 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

* 3)Now we can surely say that( V1_L1_tree )and L2 will be on left of V2

- Hill Billy November 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node * merge(Node * r, Node * s) {
    if (!r)
        return s;
    if (!s)
        return r;
    if (r->d <= s->d) {
        Node * rright = r->right;
        r->right = NULL;
        Node * l = merge(r, s->left);
        s->left = l;
        return merge(rright, s);
    }
    else {return merge(s, r);}
}

- -db August 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

This can be done in linear time and constant extra space
There are three steps:
Suppose number of nodes in first BST is n and in Second BST is m.

Step 1:: Convert both the BSTs to DLL             this takes O(n) + O(m) time and O(1) space
Step 2:: merge the two DLLs in place              this takes O(n+m) time and O(1) space
Step 3:: Convert the merged DLL to BST          this takes O(n+m) time and O(1) space

Step 1 and 2 are very easy.
Step 3 is the trickiest one , refer to this post :: geeksforgeeks.org/archives/18611

- anurag.gupta108 August 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just delete elements from one tree, and insert them into the other.

- IntwPrep October 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

inorder operation for both Trees.
Merge two sorted array.
Create a BST by the merged array.

- etlds October 06, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More