Amazon.com Interview Question
1,572 Interview Reviews |
Back to all Amazon.com Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Amazon.com:
If you have a file containing millions of integers, how would you sort the data in the file using extremely limited resources, such a s 1GB of memory?
| Tags: | sorting algorithm, scaling See more , See less 8 |
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (7)
OK, to answer the intended question, I would solve it with statistics. Compute bucket ranges by using a random subset of the file, with bucket size chosen such that the expected portion of the entire file will fit within the memory available. Divide the file into these buckets, and sort the buckets individually. Concatenate the sorted buckets to create a single sorted file.
Helpful Answer?
Yes |
No
Inappropriate?
You missed one step. Concatenating buckets will not make a sorted list. You have to merge the buckets. However, for merging you do not necessarily have to load the whole bucket into memory.
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
read the n number of integers and write them in a binary tree. hold the left and right value in a datastructure which keeps track of the file an integer is written to.
something like this
class NumHolder {
int min;
int max;
String fileName;
}
Map partInfo = new HashMap()
for (int i=0; i<1...; i++) {
int[] array = readNNumbersFromFile()
sortAndWrite(array) //updates the partInfo map with NumHolder data
}
readFromAllPartFilesAndSaveToNewFile(partInfo)
void sortAndWrite(array) {
sort(array);
if array[0] - array[last] > 100000 //100000 = sum number that determines how many part files will be created.
writeToOtherFileToo
writeToPartFile
update partInfo with this write
}
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 2 people found this helpful
by Duy:
In my opinion, I would answer this way:
Sort each x integers (x is the number of integers that the memory can hold) then save it to the a file (n files) . Then for each file maintain a current index initiated with 0. Loop through n file and take the max integer, put it into the result file, and increase the file's current index to one. Continue doing it till the end.