Reputation:
I have 1 million queries on disk to sort, my local memory/cache can store
up to 1 thousand queries, how can I perform sort?
This is a google interview question. Can someone help me find the answer?
Upvotes: 1
Views: 528
Reputation: 2505
I am lazy: Read all queries (1,000 each), dump them in a database, then SELECT ORDER BY anything
. If it is Google asking, they'll have databases anyway...
Upvotes: 1
Reputation: 179907
Get more memory. A query is not that big. It's a question that tests whether you can think outside the box.
Upvotes: 1
Reputation: 258618
You load 1000 queries in memory and sort them using quicksort, writing them to a file. In the end you'll have 1000 such files.
You then go on to merging the files using, you guessed it, mergesort.
That's how I'd do it. By no means the most optimal solution.
Upvotes: 4