Amazon Interview Question for Software Engineer / Developers






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

First write a function that determines whether a tree is a subtree of another. Then recursively apply it on both trees: 1 subtree of 2 or 2 subtree of 1. If the function evaluates to false, do the same with left child of 1 with 2 and left child of 2 with 1 and same with right subtrees..

- Ashish Kaila June 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I doubt if it's possible in linear time. lol's solution works for some specific case. Here's an example to show the difference:

______________10______________
               /                              \
        _______3______                  ______14      
       /              \                /               
       0            ___6__            12                  
                   /      \                                
                   4       7                                
                    \       \                                
                     5       8                                
        _______8______
       /              \
    ___3__            10__
   /      \               \
   1      _5              14
         /  \            /   
         4   7          12    
         
largest common BST exists at node : 10 of size 3

However, lol's solution gives output subtree rooted at 14 (of size 2). It depends what interviewer exactly asked for. To achieve above output, it's straight forward to get the solution in O(nm) time.

- buried.shopno June 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@buried.shopno: I understood lol's solution. Can you explain your algorithm? (which gives the correct answer as 14).

- Anonymous June 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well, there are basically 4 cases to handle:

Let, r1 = current node of 1st tree
     r2 = current node of 2nd tree

Case 1 : r1->data < r2->data
     2 subproblems to solve:
     first, solve r1 and r2->left 
     second, solve r1->right and r2
Case 2 : r1->data > r2->data
     similar
Case 3 : r1->data == r2->data
     2 cases to consider here:
     (a) current node is part of largest common BST
         compute common subtree size rooted at r1 and r2

     (b)current node is NOT part of largest common BST
         2 subproblems to solve:
         first, solve r1->left and r2->left 
         second, solve r1->right and r2->right

Why case (a) and (b) are different? Look at below 2 examples:                        ______________10______________
               /                              \
        _______3______                  ______14______
       /              \                /              \
       0            ___6__            12              18  
                   /      \                                
                   4       7                                
                    \       \                                
                     5       8                                                ______________10______________
               /                              \
        _______2______                        14______
       /              \                               \
       1            ___6__                          __18  
                   /      \                        /       
                   4       7                      16        
                            \                                
                             8                                
largest common BST exists at node : 6 of size 4
Here, root (10) is not part of largest common BST                                ______________10______________
               /                              \
        _______3______                  ______14______
       /              \                /              \
       0            ___6__            12              18  
                   /      \                                
                   4       7                                
                    \       \                                
                     5       8                                                ______________10______________
               /                              \
        _______2______                        14______
       /              \                               \
       1            ___5__                          __18  
                   /      \                        /       
                   4       7                      16        
                            \                                
                             8                                
largest common BST exists at node : 10 of size 3
Here, root (10) is part of largest common BST

- buried.shopno June 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@buried.shopno: thanks! Awesome solution man! You rock! :)

- Anonymous June 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can someone provide the code for this..

- swathi June 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome Solution !!!!

Here is the code :

int countCommonNodes(node *head1,node *head2){
if(head1==NULL || head2 ==NULL){
return 0;
}
if(head1->data == head2->data){

return 1 + countCommonNodes(head1->left,head2->left) + countCommonNodes(head1->right,head2->right);

}
else{
return 0;
}
}

void findMaxSubBST(node *head1,node *head2,int *ans){

if(head1 == NULL || head2 == NULL){
*ans = max(*ans,0);
return ;
}

else if(head1->data > head2->data){
findMaxSubBST(head1->left,head2,ans);
findMaxSubBST(head1,head2->right,ans);
}else if(head1->data < head2->data){
findMaxSubBST(head1,head2->left,ans);
findMaxSubBST(head1->right,head2,ans);
}else{

*ans = max(*ans,countCommonNodes(head1,head2));
findMaxSubBST(head1->left,head2->left,ans);
findMaxSubBST(head1->right,head2->right,ans);

}

}

- lesnar 56 June 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanks man.. you rock..

- swathi June 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice Algo

- KEEG June 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Overall nice solution and code as well.

However, I have a small doubt, do we need the second function "countCommonNode()" at all?

when current head1 == head2, recursively solve left and right? Isn't this enough?

Can someone please answer my doubt?

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

oops got it now.

When 2 nodes match, from then onwards processing will be different unlike the current function. So we definitely need another function.

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

nice solution...:)

but this will give the size of common bst's, how to find the nodes for that?

- Anonymous July 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

complexity seems to be exponentials ... is this the optimal solution ??.. so far i guess !!!

- exponential algo September 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do u think that : countCommonnodes() method check for real common BST nodes.What i mean is , a BST must have some internal and some external nodes. But above mentioned function counts for similar nodes no matter whether largest ans till now contains leaves or not. ans in above solution may contain internal nodes of both trees which satisfy BST property but what about leaves??
Correct me if i am wrong.

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

Do u think that : countCommonnodes() method check for real common BST nodes.What i mean is , a BST must have some internal and some external nodes. But above mentioned function counts for similar nodes no matter whether largest ans till now contains leaves or not. ans in above solution may contain internal nodes of both trees which satisfy BST property but what about leaves??
Correct me if i am wrong.

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

The code return both nodes and count:

struct item
{
TNode* node1, *node2;
int count;
}
;

item func(TNode* r1, TNode* r2)
{
if(r1 == NULL || r2 == NULL) { item it; it.node1 = NULL; it.node2 = NULL; it.count = 0; return it; }

if(r1->id < r2->id)
{
item i1 = func(r1, r2->left);
item i2 = func(r1->right, r2);

item it = i1; if(i1.count < i2.count) it = i2;
return it;
}

if(r1->id > r2->id)
{
item i1 = func(r1->left, r2);
item i2 = func(r1, r2->right);

item it = i1; if(i1.count < i2.count) it = i2;
return it;
}

item left = func(r1->left, r2->left);
item right = func(r1->right, r2->right);

item it = left; if(left.count < right.count) it = right;

if(left.node1 == r1->left && left.node2 == r2->left && right.node1 == r1->right && right.node2 == r2->right)
{
it.node1 = r1; it.node2 = r2;
it.count = left.count + right.count + 1;
}

return it;
}

int main()
{
TNode* r1 = initbst(); TNode* r2 = initbst(); r2->id = 100;

item it = func(r1, r2);
}

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

Awesome answer :)
Thanks..

- vind June 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Time: O(nlogn)

void maxCommonBST(node* root1, node* root2, node* curRoot, node** comRoot, int curCount, int* totalCount)
{
	if(root1&&root2)
	{
		if (root1->data==root2->data)
		{
			curCount+=1;
			if(curRoot==0)
			{
				curRoot=root1; /*could be assigned to root2 also*/
			}
			maxCommonBST(root1->left,root2->left,curRoot,comRoot,curCount,totalCount);
			maxCommonBST(root1->right,root2->right,curRoot,comRoot,curCount,totalCount);	
			if(curCount>(*totalCount))
			{
				*totalCount=curCount;
				*comRoot=curRoot;
			}
		}
		else if (root1->data<root2->data)
		{
			maxCommonBST(root1,root2->left,0,comRoot,0,totalCount);
		}
		else
		{
			maxCommonBST(root1,root2->right,0,comRoot,0,totalCount);	
		}
	}
}

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

it does not work

- Anonymous July 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

y?

- 123 July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's an elegant solution given by buried.shopno
careercup.com/question?id=9577777

- anonymous July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Store the inorder traversals of both the BST.
find the longest common subsequence

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

this is good solution can be implemented in n log(n). Thanks for the hint

- codemonkey June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i am sorry it O(n)

- codemonkey June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anonymous: I don't think your solution is correct at all. how could you extract the STRUCTURE of tree from 2 BSTs just by their inorder traversal.

@codemonkey: Did you really feel it's a sound solution. Then pls explain.

3
   /
  2
 /
1
inorder : 1-2-3


    2
   / \
  1   3
inorder : 1-2-3


  1
   \
    2
     \
      3
inorder : 1-2-3

Now, if I give you first and last BST, output should be single node which is common (by dafault). How do you get it?

- anon June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anon:it can be done just use place holders like l for left r for right for all the intermediate null pointers while doing the inorder traversal...in ur case that wud be l1r2r3r and l1r2l3r

- Sathya June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sathya: yes I know that is possible to represent a tree uniquely. But, still I think it's hard to extract largest BST from this representation. For the example, how could you find largest bst from "l1r2r3r" and "l1r2l3r". Thanks.

- anon June 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

As I interpret this question as "finding the common subtree among given BSTs", we can do following steps:

1. make preorder traversal of each BST.

2. find largest common substring of 2 preorder traversals.

Here the assumption is that keys in each tree are unique. Using suffix tree, above problem can be solved in O(n) time. Otherwise, we could use DP that takes O(n^2).

- lol June 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a correction : "finding the common subtree of largest size among given BSTs"

- lol June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

another clarification: I use this definition for subtree (from wikipedia)

A subtree of a tree T is a tree consisting of a node in T and all of its descendants in T

If a subtree may exclude some of its descendants, then above approach fails.

- lol June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"using suffix tree above problem can be solved in O(n)" okay!!!

- baghel June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

did u even read the question?

- baghel June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, I read the question. I never reply to a question without reading it (and trying to understanding to the best of my knowledge). Would you mind to elucidate me at which point I missed the original question? Thanks.

- lol June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just to clarify, I meant suffix tree can be used to find largest common substring problem(step 2). Though, suffix tree usually considers alphabet set to be ASCII chars, I don't see any problem to adapt it to BST where keys are integers. What's your thoughts/doubts here?

- lol June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

srry dude. i was too rude.
actually the question is asking for subtree that is common for both the tress. i.e. nodes orientation should be preserved in that subtree as shopno later pointed out.
secondly i guess its not kinda possible to code whole suffix tree stuff during the interview. i guess u better not mention it during the interview coz the very next thing he got gonna ask u to code the suffix tree LOL(my personal exp :P )

- baghel June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

srry dude. i was too rude.
actually the question is asking for subtree that is common for both the tress. i.e. nodes orientation should be preserved in that subtree as shopno later pointed out.
secondly i guess its not kinda possible to code whole suffix tree stuff during the interview. i guess u better not mention it during the interview coz the very next thing he got gonna ask u to code the suffix tree LOL(my personal exp :P )

- baghel June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No problem :)
Thanks for your suggestion. I agree it's too hard to code complete suffix tree stuff (correctly) during an interview period. I agree with buried.shopno's idea. In his mentioned case, I think we could come up some recursive approach that would take O(n^2) time.

- lol June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you both are gone mad.. how can you say that both tress are similar if preorder is same? check the above comments..
-----2
---1---3 (tree1)

----3
--2
-1 (All are left childs, tree2)

Tree1 and tree2 produce the same preorder but they are not same..

- swathi June 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@swathi: before terming someone "mad" check out your understanding of BST definition. Your 2nd tree is NOT a BST.

FYI, BST serialization/deserialization can be done using only preorder traversal.

- lol June 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, I did a stupid mistake there. Both of the trees are VALID. But the preorder traversals are different :
tree 1 : 2 1 3
tree 2 : 3 2 1

Hence, max common is of size 1.

- lol June 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@lol u don't have to pay attention when a girl is talking about programming,just ignore her LOL

- baghel June 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

baghel and lol, i made a silly mistake in drawing the trees but that should be a common sense to graspe..

---3
--2-1 (tree1)

---3
--2
-1 (tree2)

both the trees has the same preorder.. so you can't tell that if preorder is same then trees are similar..

- swathi June 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

again :D.... Check out your definition of BST. Your first input is INVALID. How could 1 go to right of 3!

Rather finding if someone is wrong, it's always better to first think deeply, and re-check oneself. As I already told you that "BST serialization/ deserialization can be done using only preorder traversal" - did you ever try to understand why it holds?

- lol June 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@lol
----3
--1
---2

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

@lol
BST serialization/deserialization can't be done with preorder alone.

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

I stand CORRECT. Your understanding is pretty wrong. For above case preorder gives "3-1-2". It says

3 is root
1 is left child of 3
2 < 3, so go to left, 2 > 1, so 2 is right child of 1

If you wanna challenge, give more complex examples.

- lol July 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I second lol's idea is correct, based on his assumption.
Where are those 2 stupid posters (posted on July 7) who dared to challenge lol with their idoicity? Crappy people are roaming here for no reason...

- anon July 27, 2011 | Flag


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