Which data structure is most efficient to find the top 10 largest items out of…
2024
Which data structure is most efficient to find the top 10 largest items out of 1 million items stored in a file?
- A.
Min heap
- B.
Max heap
- C.
BST
- D.
Sorted array
Show answer & explanation
Correct answer: A
Concept:
When only the top K elements are needed out of a much larger collection (K much smaller than N) and the assumption is a single pass over the data with only a small, bounded amount of working memory — the realistic constraint for a very large file that cannot be loaded entirely into RAM — the efficient approach is to maintain a bounded min-heap of size K instead of sorting or fully ordering the whole collection. The heap's root always holds the current minimum among the K candidates kept so far, so a new value only has to beat that single root to earn a place — giving O(N log K) time and O(K) extra space, rather than O(N log N) time and O(N) space for a full sort or a full-size heap held entirely in memory.
Application to this question:
Read the first 10 items from the file and build a min-heap out of them.
For each of the remaining 999,990 items, compare it against the heap's root (the smallest of the current top 10).
If the item is smaller than or equal to the root, discard it — it cannot belong to the top 10.
If the item is larger than the root, replace the root with this item and sift it down to restore the min-heap property.
After all 1,000,000 items have been scanned, the heap holds exactly the 10 largest items in the file.
Cross-check:
This scales far better than fully sorting the file: a complete sort of all 1,000,000 entries costs O(N log N) comparisons even though 999,990 of those entries never make the final top 10, while the bounded heap does only O(N log 10) work and never needs to hold more than 10 items in memory at once.