Interview Question for Software Engineer / Developers


Country: -
Interview Type: Phone Interview




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

Find the preorder traversal of both lists , this gives the list in ascending order.
Copy it in an array and then call BST with the array
Running time
O(n) + O(m) + c(m+n) + O(m+n)
where n- no of elements in tree 1
m= no of elements in tree2
c- constant time to copy each element to an array
O(m+n) - time for BST algo to construct a BST

- Ali_BABA July 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ ali baba i think you want to do inorder traversal for getting sorted sequences , preorder traversal is not going to give sorted sequence.

- zeroByzero July 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes.. ALI_BABA u have to do inorder

- Chalis_chor July 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"O(m+n) - time for BST algo to construct a BST"

Doesn't this require O(nlogn) time instead of O(m+n)? Correct me please if I am wrong?

- Khul_ja_sim_sim July 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok I get it, you will create the BST in O(n+m) since the array is ascending. BUT it would be more like a list than a BST.

- Khul_ja_sim_sim July 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I dont think merging to binary search tree can be done in log(n) time, it needs O(n) time

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

For doing this in O(logn) time, can't we just traverse the smaller BST, and insert the data into the larger one?

- Bonzo July 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That would take O(nlogn) time.

- Animesh Sinha July 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oh, yes, you're right. So I guess there is no way to do this in O(logn) time then, is there?

- Bonzo July 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I read some where that AVL trees can be merged in O(logN) time, but could not find out the algo,
Also treaps (see wikipedia page on Treaps) can be merged/split in O(LogN) time.... Still no concrete algo

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

Flatten the two BST's by traveling inorder and store the results in arrays.
merge two arrays.
build a BST with the merged array.

- Spock July 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In order traversal of a tree takes O(n) time. Your approach will take approximately O(nlogn) time. The interviewer asked for a O(logn) time algo.

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

Why would it take O(nlogn) time? Let's say BST1 contains m nodes and BST2 contains n nodes.

inorder traversal --> O(m + n)
merging two arrays --> O(m + n)
construct one BST containing all nodes --> O(m + n)

- Anonymous July 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Can't we just insert one BST in another BST like we insert a new element in BST.
It will take O(logn) time and also the BST property will be maintained in final tree

- bagarwal July 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No because for n elements of one tree the complexity would become O(nlogn); unless first binary tree contains elements in order O(c).

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

@bagarwal I think your method could work.........just insert the topmost element of 1 BST to another BST.....this can be done in O(log(n)) time.......and let the remaining elements attached to that element...

- Water July 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No suppose we have a(T1,T2) and b(T3,T4) as 2 BSTs with a and b as roots and Ti's as child BSTs.

Now suppose b is less than a then b is inserted somewhere in T1. Now if we just add T3 and T4 below b in T1 maybe an element of T4 could come that is greater than a which would violate the BST property

- Himanshu July 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok.....I think I made a mistake....Thanks for pointing out...

- Water July 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

May be O(logn) not possible

- kol July 11, 2012 | 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