Reputation: 13
Yesterday, I attended an interview for network security position and this question was asked:
Let's say you have a PC with 1GB RAM and the disk of this computer holds a 10 GB file which contain random numbers. What technique will you use for sorting the file and propose algorithm. You cannot be using the disk or network or virtual memory for sorting?
I tried many different ways as I could think of, proposed External sorting but the interviewer said that this was not the right way. At the end of the interview I ask him politely what is the way and the algorithm for the question he ask me but he refused to say like it was some kind of big secret.
My question is how would anyone approach this kind of problem cause i just can't stop thinking about it but still no clear answer?
Upvotes: 1
Views: 590
Reputation: 848
To sort the file you will need to run your algorithm inside the RAM. Because the file is 10x larger than the available amount of RAM, you will need to sort the file into 10 (or more) separate chunks, and recombine them at the end.
Your algorithm would need to consist of..
Update: I'm adding to my answer to reflect some points made by @JimMischel
The algorithm in RAM would not just be keeping track of the biggest number, but also maintaining a separate integer count that would be incremented with every subsequent occurrence of the number on the file. The number would then be placed on the sub-list in RAM however many times the number occurs.
Update: Concerning the asker's question
The question posted by the OP states that "you cannot use the disc for Sorting". The question says nothing that would imply disc cannot be used for storage. I believe most reading the question interpreted this the wrong way and thus, without having a place to store any data, presumed the assigned task to be impossible.
Upvotes: -2