Adobe Interview Question for Computer Scientists


Country: India
Interview Type: In-Person




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

I the question its written binary tree but all are discussing about BST , can any one clear it .. is that BST or Binary Tree.

- Nitin May 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if they are binary trees than add at any leaves. In case of BST than the complexity would be mlogn or nlogm .

- tomb February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Perform In-order traversal on the smaller tree, let's say the tree of size m (m<n). Store the now sorted keys => Complexity = mlog(m)

2) Insert the m (unnecessarily sorted) keys into tree n => Complexity = (m+n) log (m+n).

Complexity of 2 steps is the sum.

(?)

- Nix February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

we should convert the binary trees to lists and finally merge.

- googlebhai February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yup, this seems like the correct answer. Complexity would be O(m+n).

- gimli February 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you also need to create tree and that will be very complexity KLog(K), where K is m+n, so evenyually it will be more complex then nlogm

- abhishekatuw February 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

for creating lists do we need to take the inorder and preorder/postorder traversals?
If so what would be the final preorder/postorder traversals after merging the lists so that we can get back a tree?

- Anirban Datta March 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can anyone give an example of merging, i am not able to underestand what merging is

- ishu July 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node *mergebst (node *root1, node *root2)
{
stack s1;
stack s2;

while (!root1)
{
s1.push(root1);
root1= root1->left;
}

while (!root2)
{
s2.push(root2);
root2= root2->left;
}

int m= no of element in bst1
int n= no of ellements in bst2
return mbst( &s1, &s2, (m+n);
}

node * mbst( stack *s1, stack *s2, n)
{
node *ret = NULL;
if (n<=0)
return NULL;

lc = mbst (s1, s2, n/2);

node * p1 = s1->top()
node * p2 = s2->top()

if ((p1->data <= p2->data) || p2 == underflow)
{
p1 = s1->pop();
ret = p1;
p1 = p1->right();
while (!p1)
{
s1->push(p1)
p1=p1->left;
}
}
else { ..do the same for p2..}

rc = mbst (s1,s2, n-n/2-1);
ret ->left = lc;
ret->right = rc;
return ret;
}

- Anonymous1 March 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(m+n)

- Anonymous1 March 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algo :

1. Convert the two binary trees to DLLs
2. Merge Two DLLs
3 Convert merged DLL to Binary tree.

Overall Worst Case : O(m+n)

- Anonymous August 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 5 vote

if we have two trees of length m and n, say m < n,
everytime we remove one element from m tree and insert inton n tree.
the complexity of each insertion will be log(n+i), i will be extra addition to tree.
total complexity should be
log(n) + log(n+1) +...+ log(n+m)
= log(n * (n+1)*..*(n+m)) = log((n+m)!) - log((n-1)!)

- sid April 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Start from the root of say BST1, and search for its correct position in other tree BST2. Let this be node x, now if root of BST1 lies to the right of x, then attach the root as well as whole right sub-tree at this node, and perform the same operation with left sub tree as BST1.
Thus, we will do a search (O(lg n)) in BST2 O(lg m) time which is the height of BST1.
Hence complexity is O(lg m . lg n).

- gimli February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Missed to mention, if the position of root lies to the left of the searched node x, then attach the root and the left sub-tree at this node, and continue with right sub-tree as BST1.

- gimli February 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

By bad, the solution is incorrect :(

- gimli February 21, 2012 | 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