Google Interview Question for Software Engineer / Developers






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

1. Flatten the trees to DLLs -O(log n)
2. Loop through 'em - O(N)
3. Revert to trees. O(log n)

O(N) complexity game.

- Mohan February 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In order traversal,I would think is sufficient.
In order traversal of a binary search tree would always provide a sorted array of the elements.So just check if that is same.

- amit November 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question is "if same is defined as BSTs contain same sorted int array", so the interviewer didn't worry about shape. The question itself contains the the simples solution i.e do inorder traversal and compare the results, which would need O(m+n) additional space. I guess, he might ask a solution without using additional space.

1> If no of elements in both the trees are same, then try searching elements of on BST from other, if you don't find it its not same. O(nlogn) + 2n i.e nlogn

- Mr Bean November 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Mr Bean it would be O(2*n*logn) + O(2n) = O(2*n*logn) = O(n*logn)
Reason: If you only check if all the elements of BST-1 are present n BST-2 and not check if all the elements of BST-2 are also present in BST-1 then you might get wrong answers if BST-1 contains DUPLICATES but BST-2 doesn't contain any DUPLICATES!

- victoriousvishal September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question is "if same is defined as BSTs contain same sorted int array", so the interviewer didn't worry about shape. The question itself contains the the simples solution i.e do inorder traversal and compare the results, which would need O(m+n) additional space. I guess, he might ask a solution without using additional space.

1> If no of elements in both the trees are same, then try searching elements of on BST from other, if you don't find it its not same. O(nlogn) + 2n i.e nlogn

- Mr Bean November 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do you really save space? I think that in order for you to get the numbers from the first BST, you need to traverse it. What do you think?

- Aytekin December 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Mr Bean it would be O(2*n*logn) + O(2n) = O(2*n*logn) = O(n*logn)
Reason: If you only check if all the elements of BST-1 are present n BST-2 and not check if all the elements of BST-2 are also present in BST-1 then you might get wrong answers if BST-1 contains DUPLICATES but BST-2 doesn't contain any DUPLICATES!

- victoriousvishal September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

in the figure is 5 and 10 seems to be interchanged? If you want to check if thy are same why can't we traverse together on both trees?

- Annonymous November 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in-order is sufficient for this problem

- pansophism December 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

bool IsSimilar(tree* node1, tree* node2)
{
****if(!node1 && !node2)
********return true;
****if( (!node1&&node2) || (node1&&!node2) || (node1->value!=node2->value) )
********return false;
****return IsSimilar(node1->left,node2->left) && IsSimilar(node1->right,node2->right);
}

- Jegan Shunmugavel December 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Good solution..:)

- Immanuel Kingston December 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please read the question.
"if same is defined as BSTs contain same sorted int array"

Your solution is the typical one for verifying if two trees have the same shape & data (same tree). Not the interviewer is asking for here.

- idiot February 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

convert the BST to doubly linked list, O(n) for two trees. Compare the link list, O(n)

- remlostime November 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just do the same operation on the binarySearchTree at every step.. during Inorder.



bool FLAG;
inorder (node *root1,node *root2) {

if(root1 == NULL || root2 == NULL ){
if(FLAG)FLAG=false;
}
else if(root1->left =NULL && root1->right=NULL ) {
if(root1->data == root2->data) return;
else { if(FLAG)FLAG=false;
}
}
else {
inorder (root1->left,root2->left);
inorder (root1->right,root2->right);

}
}

- Lohith Ravi March 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

try 10 and 10
5
1

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

try on 10->5->1 and 1<-5->10

- anand May 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Just compute inorder for both BSTs, and compare them.

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

2 trees can have the same inorder traversals
need not be true that they are same

- pranav November 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Inorder is not necessarily unique for two tree.

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

It says BST not just a tree.

- Rayden November 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Inorder is not sufficient to compare.
Need to check Inorder and Preorder traversals. If they are the same then BSTs are similar.

- limaye.nikhil November 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Only Comparing inorder traversal is not enough because it is Binary Search Tree ?

For example :

a) 20 b) 10
/ \ / \
10 40 5 20
/ \ \ / \
1 5 50 1 40
\
50

For above both three inorder will be 1, 5, 10, 20, 40, 50.
Why we need to check for Preorder traversals ?

- Anonymous November 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Corrected example is as follows :

a)       20                          b)    10
        / \                                / \
       10  40                             5   20
      / \   \                            /      \
     1   5   50                         1        40 
                                                  \
                                                  50

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

Sorry..my bad...didnot read the definition of 'is same' clearly.
Thanks for correcting :)

- limaye.nikhil November 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the above example, a) and )b produce two different sequences by InOrder Tree Walk.
a)1 10 5 20 40 50
b)1 5 10 20 40 50

- Anonymous December 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your first tree is not even a BST. please check that. How can 5 come as the right child of 10 when 5 is less than 10.

- limaye.nikhil December 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yea. Not even a valid BST!

- Anonymous December 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

How about taking level-wide travels on two trees? If finding not same nodes, return false; return true until finishing the traveling. Worst case is O(nlogn), average case would be less than O(nlogn)

- wangbingold December 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how come O(nlogn), worst case is when you check all the elements which means it should O(n)

- outofthebox December 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree. You can not check if two trees are similar without examining all N nodes that means best algorithm will be O(N)

- ss February 13, 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