Amazon Interview Question for Interns


Country: United States
Interview Type: Phone Interview




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

For the median, you keep a max heap for numbers less than the current median and a min heap for numbers higher than the current median. From here, you will base the new median based on the max and min.

Push and pop, you just keep a regular array

- Skor April 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how to manage the min and max heap when pop a element?

- CHmoonKa April 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how to do when pop a element?

- CHmoonKa April 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hashtable pointing to location in min, max heap, then remove from those

- Skor April 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

As a practical matter, this solution involves writing your own heap implementation so that you can implement custom operations like removing an arbitrary element through a pointer.

We can make the code easier to implement by using two balanced BSTs from a library instead, and not maintaining a hash table. When popping the stack, the trees will be searched to remove the element.

- Anonymous April 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

two heaps: one mean heap and one max heap. The median will be the top element from either the mean or the max heap. Then every node should have a link to the next node added after it and we should also have a pointer to the latest node added.
insertion/push: log N
1)insert a node in the proper heap so that half (or half + 1) nodes are in one heap and rest half is in the other.
2)add a link to this new node from the last added node and preserve a pointer to the new node.
deletion/pop: log N
1)Delete the last node added
2) Adjust the Heap and pointers

- s100banerjee April 05, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Make the stack entries doubly linked list elements, with each element containing Value, Prev, and Next. Prev and Next refer to memory locations within the stack. Maintain reference to current median element.

On Push:

Find and insert new element into list, starting from median
If (stack size was odd) and (new element < median)
   median = median.previous;
else if (stack size was even) and (new element > median)
   median = median.next;

On pop:

Remove element from list
If (stack size was odd) and (popped element > median)
   median = median.previous;
else if (stack size was even) and (popped element < median)
   median = median.next;

Push is O(n/2). Pop and Median are O(1)

- tjcbs2 April 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As with Skor said, this problem can be divided to 2 problems:
1. Implement a stack, just do it
2. Online median searching, use a min-heap and a max-heap

- southern9cross April 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The main data structure will actually be a pair of heaps, like Skor described. To mimic a stack's functionality, just maintain an actual stack of references to nodes in the heaps. When you pop an element off the stack, you get a reference to the node that you also need to remove from the heap. If we use Fibonacci heaps, then insert and find-median are both constant time, but deletion is logarithmic time.

- nilkn April 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

whats wrong with a simple balanced BST?? much simpler than maintaining 2heaps

- nm April 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The median is not always the root.

- zipslash July 26, 2015 | 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