Heap / Top K
Use a heap to repeatedly access the smallest or largest candidate without fully sorting everything.
The task asks for the kth largest value, most frequent items, or repeatedly selecting the next best candidate.
How to approach it
Choose a min-heap or max-heap based on what should be removed first; for top k, keep heap size bounded by k when appropriate.
Tie-breaking may matter, and a heap does not provide full sorted order for all elements.
Practice question
What is the third-largest value in [7, 2, 9, 4, 1]?
values = [7, 2, 9, 4, 1], k = 3Give your answer before revealing the explanation.Show a hint
The values in descending order begin 9, 7, 4.
Show an answer
4
Heap / Top K at work
A support dashboard shows unresolved tickets with priorities [2, 9, 4, 7, 5]. Which three should appear in the highest-priority queue?
Show one possible approach
Priorities 9, 7, and 5. A size-3 min-heap can retain the three largest values while tickets are scanned.
Practice next
Use these problem IDs to find additional practice for this pattern: