Top K And Heaps
Asked of: Software Engineer
Last updated

What's being tested
These problems test top-k selection: choosing a bounded set of best candidates without fully sorting all input. You must recognize when to use a size-k heap, BST traversal, quickselect, or a heap-backed streaming design, then explain correctness, tie-breaking, and complexity.
Patterns & templates
-
Size-
kmin-heap for k-th largest or top-k values: push each item, pop when size exceedsk;O(n log k)time andO(k)space. -
Size-
kmax-heap for k smallest or closest candidates; define the root as the worst retained item so replacement is constant-time. -
For k closest BST values, use inorder traversal’s sorted order with two pointers, predecessor/successor stacks, or a bounded heap; handle duplicates and target ties explicitly.
-
Quickselect finds the k-th order statistic in average
O(n)time andO(1)auxiliary space, but worst-caseO(n²)requires randomized pivots or median-of-medians. -
For sorted output, separate selection from presentation: extract heap contents and sort them, adding
O(k log k)afterO(n log k)selection. -
State edge conditions before coding:
k <= 0,k > n, empty input, duplicate values, non-integer targets, and whether “closest” means absolute distance. -
For k-pair generation, seed a min-heap with one candidate per row and advance the smallest pair’s row pointer; avoid inserting duplicate states.
Common pitfalls
Pitfall: Sorting everything for a top-k request works but misses the intended
O(n log k)solution and wastes memory whenk << n.
Pitfall: Using the wrong heap polarity or comparing only values can discard the better candidate when distances tie; encode the full ordering key.
Pitfall: Claiming quickselect is always linear; distinguish average-case
O(n)from worst-caseO(n²)and explain when deterministic behavior matters.
Practice these
The practice cards below cover the canonical variants—solve all of them and time yourself.
Practice questions
- Lexicographically Smallest Topological Order of a Dependency GraphLinkedIn · Software Engineer · Onsite · medium
- Return the K Smallest Pairwise Products of Two Sorted ArraysLinkedIn · Software Engineer · Onsite · medium
- Find the K Values Closest to a Target in a Binary Search TreeLinkedIn · Software Engineer · Onsite · medium
- Solve Cache, Window, and Heap ProblemsLinkedIn · Software Engineer · Onsite · medium
- Find the k-th largest element in an arrayLinkedIn · Software Engineer · Onsite · medium
- Design a Top-K search words serviceLinkedIn · Software Engineer · Technical Screen · medium
- Find k closest values in a BSTLinkedIn · Software Engineer · Technical Screen · medium
- Design Top K ranking systemLinkedIn · Software Engineer · Onsite · hard
Related concepts
- Heaps And Selection AlgorithmsCoding & Algorithms
- Heaps, Priority Queues, and Top-K SelectionCoding & Algorithms
- Heaps, Top-K, And Streaming SelectionCoding & Algorithms
- Top-K Selection, Heaps, And RankingCoding & Algorithms
- Heaps, Streaming Median, and Top-K SelectionCoding & Algorithms
- Top-K Selection And Order StatisticsCoding & Algorithms