Heap Toolkit
Interview guide for Heap Toolkit with intuition, dry run, C++ code, complexity, and practice problems
This article covers the intuition, workflow, dry run, C++ implementation, complexity, and interview usage for Heap Toolkit.
1. Intuition
A heap is for problems where you repeatedly need the smallest or largest element without fully sorting everything.
This one pattern covers:
- heapify
- kth largest or smallest
- top k frequent
- merge k sorted lists or arrays
- median from data stream
2. How It Works
- Choose a min-heap or max-heap
- Push candidates
- Pop the best priority item
- Keep heap size small if only top
kmatters
3. Pattern Recognition
Think heap when you see:
- top k
- kth largest
- repeatedly extract min or max
- merge many sorted streams
- online median
4. Dry Run Example
Input:
nums = [3, 2, 1, 5, 6, 4], k = 2Step-by-step execution with min-heap of size k:
- Push
3, 2 - Push
1, pop smallest, heap stays size2 - Push
5, pop smallest - Continue until heap contains the two largest elements
Final Output:
2nd largest = 55. Code (C++)
int findKthLargest(const vector<int>& nums, int k) {
priority_queue<int, vector<int>, greater<int>> minHeap;
for (int num : nums) {
minHeap.push(num);
if (static_cast<int>(minHeap.size()) > k) {
minHeap.pop();
}
}
return minHeap.top();
}6. Complexity Analysis
- Kth Largest:
O(n log k) - Heapify:
O(n) - Space Complexity: usually
O(k)orO(n)depending on usage
7. When to Use
- streaming top-k
- merge k sorted lists
- scheduling by priority
- median with two heaps
8. Common Mistakes
- using the wrong heap type
- sorting the whole array when
kis small - forgetting that median stream needs both balance and order
9. Variations / Extensions
- max-heap and min-heap pair for median from data stream
- heap of frequencies for top k frequent elements
- node heap for merge k sorted lists
10. LeetCode Practice Problems
Easy
Medium
- https://magicsheet.dev/questions/kth-largest-element-in-an-array/
- https://magicsheet.dev/questions/top-k-frequent-elements/
- https://magicsheet.dev/questions/merge-k-sorted-lists/
Hard
11. Key Takeaways
- Heaps are about repeated best-item access, not full ordering
- Many interview problems only need the heap to stay size
k