Heap and Priority Queue
Tactic
A heap keeps the next highest-priority item available. In interviews, heap and priority queue usually mean the same operational tool: push items, pop the current min or max.
The invariant is that the root has the best priority among all stored items. The rest of the heap is only partially ordered, which is why extracting one best item is cheap but full ordering still needs repeated pops.
Use a heap when the next item changes as you process data. If priorities are static and you only need one full sorted order, sorting may be simpler.
Value
The value is selective ordering. You do not sort every item when you only need the next best item at each step.
Direct complexity example
- Brute force: Repeatedly scan an unsorted list to find the next best item: time over
nextractions. - With this tactic: Build a heap and pop as needed: heapify plus per push or pop.
- Space: Space is for the heap. For top-K with a bounded heap, space can be .
Challenges this solves
- top K selection
- merge sorted streams
- median from data stream
- task scheduling
- Dijkstra frontier
When to use it
Use this tactic when these conditions are true:
- you repeatedly need the smallest, largest, or most urgent item
- new candidates appear while processing
- keeping all items fully sorted is unnecessary
- the active set changes over time
When not to use it
Reach for a different tactic when these warning signs appear:
- you need random access to arbitrary priorities
- all values can be sorted once and scanned
- the priority changes in place without a way to push a new entry or update safely
- K is tiny and a manual scan is clearer
Terminology clues
These prompt words often point toward this concept:
- priority queue
- heap
- top K
- smallest
- largest
- median
- schedule
- next best
Problems that use it
- 23. Merge k Sorted Lists
- 215. Kth Largest Element in an Array
- 295. Find Median from Data Stream
- 355. Design Twitter
- 621. Task Scheduler
- 703. Kth Largest Element in a Stream
- 973. K Closest Points to Origin
- 1046. Last Stone Weight