Heap
O(log n)Use a heap (priority queue) when you repeatedly need the minimum or maximum of a changing set while also inserting and deleting values efficiently.
When to Use
- Finding top-k frequent or top-k largest elements
- Merging multiple sorted streams or linked lists
- Scheduling tasks by earliest deadline or smallest processing time
- Shortest-path variants where you need a dynamic smallest-distance choice
Pattern Variants
Min-Heap (Priority Queue)
Keep the smallest element available in O(1) and rebalance neighbors in O(log n) after insert/remove.
Use for: Need frequent minimum extraction like earliest end time, shortest distance, or smallest remaining value
Max-Heap (Priority Queue)
Keep the largest element available quickly and drop smaller ones when capacity is limited.
Use for: Need running maximums or top-k selection while scanning a stream
Heap Selection
When sorting is overkill, keep only a bounded heap of size k and maintain the best candidates.
Use for: Top-k, kth element, and streaming order-statistics without full sort
Interactive Visualization
Step-through visualization coming soon...
This will include beginner, intermediate, and advanced examples with code highlighting.
Practice this Pattern
Top K Frequent Elements
Find the k most frequent elements using bucket sort
Kth Largest Element
Find the kth largest element using QuickSelect (partition)
Merge k Sorted Lists
Merge k sorted linked lists into one sorted linked list using divide and conquer
Meeting Rooms II
Find minimum number of conference rooms needed
Network Delay Time
Find time for all nodes to receive signal from source