Amazon Interview Question for Software Engineer / Developers






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

Max heap

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

I will use a linked list to store the incoming streams rather than a array. Because it need to have a fast insertion and deletion which is not possible with the array.

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

Above two are inefficient solutions.

if there are n numbers of which we need to find smallest first k elements, there is a solution to do this in O(n + k(log k)) if the final result should be sorted. if the final result doesn't need to be sorted then it can be done in O(n).

Refer to: http://en.wikipedia.org/wiki/Selection_algorithm

The two solutions below my post are both inefficient. Moreover, suarabhroongta2's solution is wrong and the solution using Max heap reduces to O(n log(k)).

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

Why not use a min heap,and carry out the min heap operation k times at any given point in time.

Assuming k << N,where N is the total no. of elements in the heap, the selection step would take approximately k*log(N)time.

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

I dont understand how did u get k* log(N) ...can you please explain. Like for each element from N elements if we insert into heap ...it will take (log k) for each insertion.... wont it?...or I didnt understand your algorithm clearly

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

@Augustine,

You don't have memory for this...selection algorithm.

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

@Augustine

Moreover,the selection algorithm provides a solution to the problem of finding the k-th smallest number in a list.

The problem being discussed *here* is different. Read it again.

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

@random: When we use a min heap, we don't know when to update the heap or just leave off the incoming number from the stream.
So we need to use a max heap of size "N".
Initially construct a MAX HEAP of the first "N" input numbers from the stream

updateheap(H, num) {
  if (H.GetRoot().GetValue() < num) return;
  H.DecreaseKey(H.GetRoot(), num);
}

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

Why not store in a binary search tree and output the contents in inorder tree traversal?

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

We do not know how the input will arrive,so the chances of constructing a balanced binary tree would be concern.I too thgt about using a BST but was not sure if we would have to be concerned about tree being balanced or not.

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

I had the same idea as Dumbo had,if we make use of binary tree to insert elements as they are coming, and later on go with inorder tree traversal.

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

I think the main problem here is with 'storage'. The selection algorithms expect you to have the entire list in memory and accessible. But the heap/array solution works 'on the fly'.

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

There are several parts of the question.
1) How to store the entire set of data?
Since there is no much requirement stated, we can simly use list or vector and add new item at the end.
2) How to get only the n smallest item?
A max heap or Binary search tree (BST)of size n will do the job. If total number of items is much larger than n it won't be efficient to keep all the items in max heap or BST. In stead keep only n smallest item in max heap or BST and then every time you get a new item you have to replace the max item with new item if it is greater and then rebuild the heap or BST. The time complexity will be log(n) in both cases.
3) Do you need the first n smallest items in sorted order?
Heap won't keep the items sorted whereas BST will do the job.

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

maintain a max heap of n number. if coming number is smaller than the maxm number swap the max with the incoming number and then build the heap gain to keep the max.

- nilesh April 07, 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