Heaps, Priority Queues, and Top-K Selection
Asked of: Software Engineer
Last updated
What's being tested
Top-K selection with heaps, priority queues, and ordered traversal: count or generate candidates, define an exact comparator, then return only the best k. Interviewers probe whether you can avoid full sorting when unnecessary, handle tie-breaking correctly, and adapt the same pattern to streams, coordinates, prefix search, and sorted-array pair generation.
Patterns & templates
-
Frequency map + heap — count with
HashMap, maintain size-kmin-heap;O(n log k)time,O(m + k)space. -
Comparator discipline — encode primary and secondary keys explicitly, e.g. frequency desc, word lex asc, distance asc; test ties before coding.
-
Top-K from stream — update counts incrementally, use lazy heap entries or balanced tree; avoid assuming heap entries auto-update after count changes.
-
K smallest pairs — push
(i,0)for each first-array index, pop smallest sum, then push(i,j+1);O(k log min(k,n)). -
Distance ranking — compare squared distance
x*x + y*yto avoidsqrt; watch integer overflow inJava/C++. -
Autocomplete topK — combine Trie prefix lookup with per-node cached top candidates or DFS + heap; cache improves query time but complicates updates.
-
Bottom-K variant — invert comparator or use max-heap of size
k; don’t rewrite the whole solution when only ordering changes.
Common pitfalls
Pitfall: Sorting all candidates with
O(n log n)whenkis small; say whyO(n log k)is better.
Pitfall: Implementing an inconsistent comparator, especially for equal frequencies or equal distances, causing nondeterministic output.
Pitfall: Forgetting streaming updates invalidate old heap entries; use lazy deletion by checking current count on pop.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
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
- Solve chat, grid paths, and car rentalsGoogle · Software Engineer · Technical Screen · 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
Related concepts
- Top-K Selection And Order StatisticsCoding & Algorithms
- Top-K Ranking And SelectionSystem Design
- Top-K Selection, Heaps, And RankingCoding & Algorithms
- Heaps, Top-K, And Streaming SelectionCoding & Algorithms
- Top-K, Heaps, Quickselect, And Frequency AnalysisCoding & Algorithms
- Top-K Queries And Streaming AggregationCoding & Algorithms