Amazon Interview Question






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

heap would be the most suitable implementation for this problem,
but using a BST,i think traversing in reverse inorder might solve the problem

the steps would be

1.construct the BST as when the values arrive with arrival time as the key value along with necessary balancing at each step(otherwise no point in using bst and it will end up to be a linked list).

2.do reverse inorder traversal(this will print the values in decreasing order equivalent to a stack pop operation)

order would be O(nlogn)

please correct me if i am wrong!!

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

Absolutely Correct solution,

for balancing condition we can use AVL trees,
AVL tree is a balanced binary search tree,
so using the time stamp as key for constructing AVL tree, we ll get well balanced
binary search tree, by doing reverse inorder traversal we can achiev stack operations

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

The solution to this problem is Treap, a BST as well as a heap,
TreapNode {
left, right, parent
value
priority
}
In the node keep an extra field named Priority (OR insertion Order) increment it everytime a new node comes...

So keep creating the BST based on the value and .... rotate it (kind of heapify) whenever a high priority item is inserted on a lower levels in BST untill it becomes a root
Please see Treap for better understanding:

Order of insertion and deletion would be O(logN) as we need to rotate (heapify)

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

me, I'm not able to figure what exactly you mean. How to say that the
Max in each search is the element most recently added and that it actually the TOS.

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

Keep an additional pointer with each node, which work as stack

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

Sounds good !!!

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

This solution doesn't utilize the properties of a binary tree at all... I don't think this is what the question is looking for...

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

My humble opinion:

Use an integer index to track the order of the comming nodes. For each new node, increase this index by 1 and use this integer as the identifier of the node.
Use this identifier to decide the position of the BST. When poping a node, extract the biggest one in the BST.

The problem of this strategy is to BST may be unbalanced. So, BST balancing operation should be done from time to time.

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

essentially you are creating a linked list and balancing it randomly

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

Building a treap would be a better idea...Have the nodes hold an index. Start with index 0. As you insert elements, increment the index. Build a treap with that.. This would ensure O(log n) height for the tree

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

Yes..a max tree-heap with nodes containing a field for storing index to track the order of incoming nodes in increasing order sounds like an idea. Since it is a max-heap, popping from the stack would essentially mean deleting the root node of the heap {this is the max-heap,the node with the highest index, is of interest as this is a simulation of a LIFO }and then the re-heaping may happen that shall bring the node with the next highest index-value {order of insertion} to the root.

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

After heapify the Binary tree will no longer be BST.

- nagendra.cse October 31, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Augment the BST with the actual data being kept in augmented field and the order of insertion number kept as the key.
Now keep pointer pointed to latest inserted, when u pop this one, point this pointer to the predecessor of the node deleted.

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

Have extra field in node as "node *previouslyAdded". Always have the TOP pointer pointing to last added. While poping return the node and point the TOP to previously added.

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

Best sol.

Previous solutions are creating BST based on the order noded are inserted, since the order is increasing, so it will end up with a linked list like BST.

- Jovi June 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

"me" solution, first is the best......dont know why u guys r-goo-ing

- Anonymous December 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Augment the BST with the actual data being kept in augmented field and the order of insertion number kept as the key. Then find max in each search to get the most recently added element i.e. top of the stack.

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

The insertion number will always increase and I am asuuming you are creating the BST on the basis of the insertion number. So you will end up with a Linked List and not a BST..

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

@abhimanipal Yeah, unless we use something like an AVL tree for balancing, we will end up with a linked list (or rather, a totally lopsided tree).

- tiputiger December 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

each pie of shit

- Anonymous October 08, 2009 | 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