Amazon Interview Question for Software Engineer / Developers






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

An O(log n) worst case lower bound is impossible. Consider the case where the tree is unbalanced to the point where it's one large completely-left or completely-right list. Now say k = n. In this case, no matter what strategy you use, you will have to traverse k = n nodes (no way to jump ahead in what is really a linked list).

- Anon September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming that BST is balanced, the root of the tree is N/2 element (median). If location K == N/2, then we have found the match. If greater use the same logic recursively on right subtree, else left subtree.

- geek September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

what a moron. making a stupid assumption and jumps in

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

fuck off

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

I think you don't need to traverse the entire tree. Just use a counter as you traverse from the end element, increment the counter by one while you are traversing each element and retune the element when the counter == K.
reach the end elment O(logN), find the Kth element O(K), so it's O(K+logN)

- Anonymous September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are traversing the entire tree this way!! This solution wont work ..U ve to find the Kth maximum element

- Anonymous September 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I said traverse from the END, that is: right-root-left. Not the entire tree

- Anonymous September 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its still O(n).. Because you aren't eliminating half of the solution space at each step. You still are considering both paths... O(log n) is when you divide and conquer...

- Anonymous September 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about inorder traversal of BST and then look for kth element from the end.

- MakeItWork September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

fucking asshole do you know how to read??

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

y so much hate?

- R November 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Such a Moron u are ..... God Bless U ...

- Fcker_24X7 November 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution is easy to guess when it is a balanced tree as shown above. However, I think that with tree not balanced, one has to keep information at each node e.g. Node A tell us how many nodes there are in left sub-tree and right sub-tree. With this information in mind, we can go forward and right an traversal on O(log N). In case of balanced tree this information is not required as at every node, it is easy to predict the number of nodes in left and right sub-tree.

Please suggest some better solution that does not use this knowledge and still finds the solution in O(log n)

- SolutionKarma September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do iterative in-order and process first K nodes using stack1.Do the iterative in-order for this one and the entire tree(use stack2). When there are no more elements in stack1 (or u finished processing the tree) return the value which has to be processed in stack2...This would be the Kth maximum element...

- raj September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No idea what you are saying

- Anonymous September 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Something like Inorder but with a twist ;-)

Recursively traverse right/root/left in the BST.
This way you will be travelling in descending order (unlike INORDER which is ascending).

Keep a counter and increment on each find until it reaches K.

- pk September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Cool

- Anonymous September 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution.. But it aint O(log n). It's still O(n)

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

Nice!!

- Mak January 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

worst case complexity will be O(N)

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

BST is not guaranteed to be a balanced here

- Amar September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Anon . I think that even in worst case it would be a sorted linked list and then you can balance in O(logN) and find the K'th maximum again (probably in O(log N)

- Amar September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Amar, if you could "balance" a sorted linked list in O(logN) as you suggest, so that you could get to end of a list in O(logN) total time, it would mean that a complete linked list is possible in O(logN) time, which is impossible.

- Anon September 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its good to build a array of pointers, which keep the nodes in sorted order.
One time building cost for this array is O(n), but later for accessing purpose its cost became O(1) "constant".

- Array September 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it is not possible to find the kth maximum element in BST in O(logN) time...

- kyle September 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I believe that the BST needs to be changed a little bit. For each node, an integer is added to track the elements in the subtree starting from that node. This is NOT hard because you need to go throught that node when adding a node to the BST.

That way, you can know where you should go smartly. Without this, you always have to transverse the BST tree.

- majun8cn September 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Associate weight(Wt) with each node, like root's Wt will be no of (left sub tree+ right sub tree+1)...similarly for each node. during tree formation...or else call a utility to give no of nodes. based on that determine whether if(N==Wt of node+1) return node, else if(N>Wt of left sub tree+1) recursively traverse right sub tree else traverse left sub tree

- Ankur September 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Friends, I am not sure if the trick I describe would work, but it 'sounds' promising/interesting to me:

1) Use 2 pointers starting at root and traverse the tree in reverse-inorder (right-root-left).
2) The pointers not k steps but are log2(k) apart.
3) Basically call MAXIMUM(tree) procedure twice and return the key of the delayed pointer when the first pointer reaches real max value.

Am I making sense?

- Nix September 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think solution will involve having two more data in each node , leftHeight , rightHeight , This way solution is trivial

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

O(logN) solution is impossible with BST, If you have a balanced tree then in that case you might get such solution.

- dPac September 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

start with the min element (you motherfuckers know how to do that right?) keep finding the inorder successor for k times. morons...

- Anonymous October 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

that will become kth minimum element! not Kth maximum element.

- Anonymous October 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

asshole, that will be O(k log n)

- R November 17, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

go root->right recursively till u reaches last node. That will be max element.
start finding its predecessor to K-1 times.

- lets_learn November 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is the correct answer

- asuran November 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

isn't the same in-order traversal (from backwards) and suggested by many posters before but in different form?

- Anonymous February 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(log n) is impossible to achieve with BST unless it is balanced like AVL tree or red-black tree. If it is balanced then you can find the k-th minimum in O(log n) time using order statistic tree...similarly just by reversing you ca get the k-th maximum in O(log n) time but i repeat for BALANCED TREE only.

- Abhra July 29, 2010 | 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