Skip to interview prep
Apexed

37. How do you sort a file that is too large for memory?

HardSorting

Technical

Type or dictate your answer. Your draft is saved on this device.

Show model answer

External merge sort: read chunks that fit in memory, sort each and write it out as a sorted run, then perform a k-way merge of the runs using a min-heap that holds the current head of each run. Choose chunk sizes and the merge fan-in to minimise disk passes.

How do you sort a file that is too large for memory — Data structures & algorithms interview question — Apexed