The Heap / Priority Queue Pattern
A heap keeps the smallest or largest element instantly reachable while allowing insertions and removals in logarithmic time. Keeping a heap of size k gives you the top k of a stream without ever sorting the whole input.
5
Interactive problems1
Free to openHow to recognise a heap / priority queue problem
The question says "top k", "k largest", "k closest", "median of a stream", or requires repeatedly taking the current minimum — which is what makes it the engine inside Dijkstra and k-way merges.
Time and space complexity
O(n log k) for a top-k pass against O(n log n) for a full sort. Peeking is O(1); push and pop are O(log n).
Heap / Priority Queue practice problems
Each problem pairs a worked explanation with an interactive visualizer you step through yourself, plus the implementation in JavaScript, Python, Java and C++.
Related patterns and concepts
The underlying technique is covered from first principles in the heap / priority queue lesson in the DSA visualizer curriculum. Heap / Priority Queue is one of the 16 coding interview patterns, and its problems also appear in the DSA 75 interview sprint.
