Amazon Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

Any restriction? This can be as simple as picking up a leaf node from one of the trees and compare the value of this leaf node to that of the two root nodes. The new root is the node with median value.

- Anonymous September 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If I understood it correctly, simply make an inroder traversal of the tree1 and with every node traversed, keep adding it to tree2 and then free the node. The insert method on tree2 should take care of duplicates while adding the nodes. Its pretty simple, unless balancing is required as a follow-on question. in that case, instead of inorder traversal, a breadth first traversal should do the job.

- Neo September 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are right, but it can even be done better, an O(n) solution
using extra space and also an O(n) in place solution also exits,
try for that.

- RAM September 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi RAM,

Can you give some hint?

- Prateek October 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using a recursion, they can be merged to the extent of maintaining the complete balance in the merged tree.
The routine returns the root node of the merge trees LNode and RNode

BST_MERGE(LNode,RNode){
//bottom out condition
If(LNode==Null) return RNode;
elseif (RNode==Null) return LNode;
}
//recursion
if(LNode.data>=RNode.data){
LNode.left=BST_MERGE(LNode.left,RNode);
return LNode;
}
else{
RNode.left=BST_MERGE(RNode.left,LNode);
return RNode;
}

- mdk September 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's hard for me to verify if this is correct or not. But it does not seem right ...

For starters, I am a bit puzzled by the fact that you're checking only left children? Also, is the beginning of your recursion BST_MERGE(root1, root2)? If so, imagine 2 trees, each really a linked list with only right children. Then the recursion will end almost immediately and the resulting tree will be BST if and only if all elements of one tree are <= all elements in the other tree.

Example:

Tree1:           Tree2:        Merged tree:
 1                  2                       2
   3                  4                1        4
     5                  6                3        6
                                           5

Clearly not a BST :-(

- czpete September 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using In-order traversal to make two sorted arrays and then perform a two way merging? From the final merged sorted array, rebuild a balanced BST?

- bpin September 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i was also thinking the same way

- sushantmax88 January 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keep performing a post order deletion from 1 tree and keep inserting the removed node into the second tree.

- Anonymous September 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c++" line="1" title="CodeMonkey87449" class="run-this">void merge (node *tree1, node *tree2) {
if (tree2->val <= tree1->val) {
node *right_child = tree2->right;
tree2->right = NULL;
if (!tree1->left) {
tree1->left = tree2;
} else {
merge(tree1->left, tree2);
}
if (right_child)
merge(tree1, right_child);
} else {
node *left_child = tree2->left;
tree2->left = NULL;
if (!tree1->right) {
tree1->right = tree2;
} else {
merge(tree1->right, tree2);
}

if (left_child)
merge(tree1, left_child);
}
}
</pre><pre title="CodeMonkey87449" input="yes">
</pre>

- liuxuli September 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is the complexity of this alg??

- Sash October 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

private Node merge (Node head1, Node head2)
	{
		if (head1 == null) return head2;
		if (head2 == null) return head1;
		
		if (head1.data > head2.data)
		{
			Node temp = head2.right;
			head2.right = null;
			head1.left = merge (head1.left, head2);
			head1 = merge (head1, temp);
			return head1;
		}
		else if (head1.data < head2.data)
		{
			Node temp = head2.left;
			head2.left = null;
			head1.right = merge (head1.right, head2);
			head1 = merge (head1, temp);
			return head1;
		}
		else
		{
			head1.left = merge (head1.left, head2.left);
			head1.right = merge (head1.right, head2.right);
			
			return head1;
		}
	}

- ishtiaq.jishan October 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution

- Mahesh October 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Good solution!!

- Anonymous January 11, 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