Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

For part 1 of the problem, use min-heap of size k.

For part 2 of the problem,

1. get the first k number from the stream, and the smallest number of these k number, say mink

2. store the first k number into temp file

3. continue scan the stream, put the number larger than mink into memory buffer

4. if memory buffer is full (size larger than m), merge the buffer into temp file
1. only keep the top k larger number
1. get the new mink (k-th large number)

5. empty the memory buffer, go back to step 3 until the stream end

- cy August 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
4
of 4 vote

For part1 of the problem, use a min-heap of size k. After a number is read from the stream, if the size of the heap is less than k, insert it into the heap. Otherwise, if the given number is greater than the min-value of the heap, evict the min value and insert the given number into the heap. At the end, you will have top k elements left in the heap.

For part2, let L = m/k(m<k, per the question). Here again use a min-heap, but of size k/(L+1). In the first pass, find out the top k/(L+1) elements. In the second pass, find out the next top k/(L+1), by inserting into the heap only the elements which are less than the first top k/(L+1). Like this, make L+1 passes over the data, each time capturing a window of k/(L+1) ordered elements. In the end, we'll be left with top k elements.

- Murali Mohan August 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great Solution.

However, are you sure about the value of L being m/k. I think it should be k/m.

To start with we can have the min-heap of any size as long as it fits in the memory m. Therefore, let us assume, the size of heap gets reduced by a factor d i.e. size of the heap is k/d.

Therefore, k/d <= m
i.e. k/m <= d
i.e. d can be k/m + 1
If L = k/m + 1, then d = L + 1.

- Siddhesh Shirsat August 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

When a "stream of integers" is mentioned, we don't have the numbers saved in something like an array and we should process them one by one as they arrive.

Hence, your part2 answer would actually require O(n) memory. It sounds to me one of those questions where we can't get 100% correctness given those constraints and we have to be creative and talkative with the interviewer.

- Miguel Oliveira October 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

For every m numbers from the stream, create a sorted list and write it to a file. Once you have the stream completed you would have N files each with m sorted numbers in them. This is when you would do an external merge sort on all of the files and have a final sorted list. Take the first K numbers.

- Anonymous August 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How would you do external merge sort with 'm' memory < N * m . What is the overall complexity of your solution to query the top K numbers at any time ?

- tryingtosolvemystery August 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We can assume N to be lesser than M and hence if we could open all files and read the first number, you can see which is the largest and thus write that to a new file and increment the file pointer in the file we just took the number off of. This is the merge out of the merge sort.

- ambu.sreedharan August 06, 2013 | 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