Top-K Selection And Order Statistics
Asked of: Software Engineer
Last updated

What's being tested
These problems test order statistics and top-K selection: finding the smallest, largest, median, or highest-ranked items without fully sorting everything. Interviewers expect you to choose between heap, quickselect, two-pointer, Trie augmentation, or streaming median based on input size, update pattern, and ordering rules.
Patterns & templates
-
Min-heap frontier expansion for sorted combinations — push
(sum, i, j), pop K times; typicalO(k log k)with visited-pair deduping. -
Fixed-size max/min heap for top-K — maintain K best items in
O(n log k)time; define comparator carefully for ties. -
Quickselect for unordered arrays — average
O(n)time, worstO(n^2)unless randomized; returns partitioned top-K, not sorted top-K. -
Two-heaps streaming median — max-heap lower half, min-heap upper half; rebalance sizes so median query is
O(1), insertO(log n). -
Augmented Trie top-K — store per-node top suggestions or frequency maps; prefix lookup is
O(len(prefix)), but updates may costO(len(word) * log k). -
Multi-key ranking comparator — order by frequency, distance, timestamp, lexicographic key; make comparator transitive and match required ascending/descending semantics.
-
Approximate quantiles under memory limits — use bucket histograms, reservoir sampling, or sketches when exact median storage is impossible.
Common pitfalls
Pitfall: Fully sorting
nitems for every query givesO(n log n)whenO(n log k),O(k log n), or cached metadata is expected.
Pitfall: Ignoring duplicate states in k-smallest-pairs can push
(i, j)through multiple paths and inflate runtime or output duplicates.
Pitfall: Treating median as a batch problem when the interviewer asks for streaming updates misses the required online data-structure invariant.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Implement frequency + distance top‑K queriesGoogle · Software Engineer · Technical Screen · medium
- Find top/bottom-k words in list or streamGoogle · Software Engineer · Technical Screen · medium
- Recommend top-K movies from similarity graphGoogle · Software Engineer · Onsite · medium
- Find k pairs with smallest sumsGoogle · Software Engineer · Technical Screen · easy
- Maintain k-th largest in a streamGoogle · Software Engineer · Onsite · Medium
- Design autocomplete with TrieGoogle · Software Engineer · Onsite · Medium
- Maintain streaming median and loosemedianGoogle · Software Engineer · Onsite · Medium
- Scale median under memory constraintsGoogle · Software Engineer · Onsite · hard
Related concepts
- Heaps, Priority Queues, and Top-K SelectionCoding & Algorithms
- Top-K Selection, Heaps, And RankingCoding & Algorithms
- Top-K Ranking And SelectionSystem Design
- Top-K, Heaps, Quickselect, And Frequency AnalysisCoding & Algorithms
- Heaps, Top-K, And Streaming SelectionCoding & Algorithms
- Top-K Queries And Streaming AggregationCoding & Algorithms