Amazon Interview Question for SDE-2s


Country: India
Interview Type: Phone Interview




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

you mean split, sort and merge (concatenation wouldn't necessarily produce a sorted result)

first pass: read 10 * 1GB sort in memory, write to disk (10 files)
second pass: open the 10 1 GB files (as streams), create a new file and merge the content of the other files into the one target file (pick the smallest record on top of each stream and write it to the target) until done. then delete the 10 intermediary files.
first pass: O(n*lg(n)) second pass, O(n): total O(n*lg(n)) but due to disc activity, a bit slow

- Chris June 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Split and sort them 2 at a time.

- rocky June 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Split and sort 2 at a time

- anon June 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

split, sort, concat.

- aarav June 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

External Sorting
External sorting is a term for a class of sorting algorithms that can handle massive amounts of data. External sorting is required when the data being sorted do not fit into the main memory of a computing device (usually RAM) and instead they must reside in the slower external memory (usually a hard drive). External sorting typically uses a hybrid sort-merge strategy. In the sorting phase, chunks of data small enough to fit in main memory are read, sorted, and written out to a temporary file. In the merge phase, the sorted sub-files are combined into a single larger file.

- sagar.cdafle June 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Was this question about the strategy, or did you have to produce the code?

- persspolice June 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We have to produce a code , not the code for file related stuff but the code for the sorting the file only

- ashishsaraswat.iips June 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is the mergesort that keeps the divide part on hd.
You probably can use the quicksort, as well... I just think that mergesort looks like more in the spirit of the exercise

- Andrea Lorenzani July 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is very easy if you know N-way merge.
Following are the steps:

1) Make 10 chunks of the file, of 1 GB each.
2) Sort each of them individually using Insertion sort because insertion sort works the best when the value of n is small but it might not be the case in this one. In that case, use merge sort which is O(nlogn) in worst case. Tip: I would ask the interviewer to tell me if I have auxiliary space of O(n) because merge sort requires that.
3) After these chunks are sorted, take 1GB/(10+1) of data from each chunk into the memory. 10+1 is in the denominator because we want to have some space to store the temporary output.
4) Start a 10-way merge algorithm.
5) As soon as the output buffer is full, write it to the file in the disk.
6) When there is a vacancy in the input buffer, fetch the next chunk.

- Himanshu Jethawa July 16, 2017 | 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