Heap (Priority Queue)
Heaps are binary tree-based data structures that efficiently maintain the minimum or maximum element. They enable O(log n) insert and O(1) peek operations, making them ideal for priority queues and top-K problems.Core Concepts
Heap Types
- Min Heap: Parent ≤ children, root is minimum
- Max Heap: Parent ≥ children, root is maximum
- Python’s
heapqis a min heap by default
Key Operations
- Insert: O(log n) - add element and bubble up
- Peek Min/Max: O(1) - access root
- Extract Min/Max: O(log n) - remove root and heapify
- Heapify: O(n) - build heap from array
When to Use Heap
- Find top K elements
- Maintain running median
- Merge K sorted structures
- Scheduling/priority tasks
- Best/worst elements in stream
Key Patterns & Techniques
1. Top K Elements
- Use min heap of size K
- For K largest: push all, pop if size > K
- For K smallest: use max heap (negate values)
- Time: O(n log k), Space: O(k)
2. K-Way Merge
- Merge K sorted lists/arrays
- Use heap to track smallest from each
- Time: O(N log k), N = total elements
3. Two Heaps (Median)
- Max heap for smaller half
- Min heap for larger half
- Balance heaps to find median in O(1)
4. Heap as Sorted Structure
- Better than sorting for partial results
- O(n log k) vs O(n log n)
Common Approaches
Top K Frequent Elements
Kth Largest Element
Merge K Sorted Lists
Find Median from Data Stream
Random Pick with Weight
Problems in This Category
006. Kth Largest Element
Medium | Frequency: 86.9%Min heap of size k to track k largest elements.
015. Top K Frequent Elements
Medium | Frequency: 77.9%Bucket sort O(n) or heap O(n log k) for top k by frequency.
019. Merge k Sorted Lists
Hard | Frequency: 76.4%Heap tracks minimum across k lists for efficient merge.
082. Find Median from Data Stream
Hard | Frequency: 40.7%Two heaps maintain balance for O(1) median queries.
034. Sliding Window Median
Hard | Frequency: 65.0%Two heaps with removal tracking for window median.
012. Random Pick with Weight
Medium | Frequency: 80.5%Prefix sum with binary search for weighted random selection.
091. Random Pick Index
Medium | Frequency: 32.0%Reservoir sampling for random selection with equal probability.
Complexity Characteristics
| Operation | Time | Space | Notes |
|---|---|---|---|
| Insert | O(log n) | - | Bubble up |
| Peek Min/Max | O(1) | - | Access root |
| Extract Min/Max | O(log n) | - | Remove and heapify |
| Heapify Array | O(n) | O(1) | Build heap |
| Top K | O(n log k) | O(k) | Better than sort O(n log n) |
| K-way Merge | O(N log k) | O(k) | N = total elements |
| Two Heaps Median | O(log n) add, O(1) median | O(n) | Maintain balance |
Interview Tips
Pattern Recognition
- “Top K elements” → Min heap of size k (for largest) or max heap (for smallest)
- “Kth largest/smallest” → Min/max heap of size k
- “Merge K sorted…” → Heap with k elements
- “Running median” → Two heaps (max for small, min for large)
- “Closest K points/elements” → Heap with distance comparator
- “Frequency-based top K” → Counter + heap or bucket sort
Common Mistakes
- Using max heap in Python without negating values
- Not maintaining heap size k (memory grows to n)
- Forgetting to heapify when building from array
- Confusing heap[0] (min) with sorted array[0]
- Not handling ties in comparisons (use tuple with tiebreaker)
Key Insights
- Min heap of size k for k largest - counterintuitive but efficient
- Heap is partial order - only root is guaranteed min/max
- O(n log k) vs O(n log n) - heap wins when k << n
- Two heaps for median - balance property enables O(1) query
- Heapify is O(n) - better than n insertions O(n log n)
- Python heapq is min heap - negate for max heap behavior
Python heapq Tips
Basic Operations
Max Heap in Python
Custom Comparators
Advanced Patterns
Lazy Deletion for Sliding Window
Heap with Custom Objects
K Closest Points to Origin
Pro Tip: For top K problems, use a min heap of size k to find k largest (or max heap for k smallest). This gives O(n log k) instead of O(n log n) for sorting, which is much better when k << n.