Heap FAQs
Common heap problems categorized by pattern, with difficulty and LeetCode links.
Heap Problem Patterns
Heap problems fall into 4 recurring patterns. Mastering these covers nearly every heap question asked in interviews. Click a problem name to open its full solution walkthrough (where available), or use the LC link to open it on LeetCode.
Top K Pattern
Problems where you need to find the k largest, smallest, or most frequent elements. A heap of size k is the key tool.
Merge K Sorted Pattern
Problems where you need to merge or pick from multiple sorted sequences. A min-heap lets you efficiently always pick the smallest next element across all sequences.
| Problem | Difficulty | Link | |
|---|---|---|---|
| 373. Find K Pairs with Smallest Sums | Medium | LC | |
| 378. Kth Smallest Element in a Sorted Matrix | Medium | LC | |
| 23. Merge K Sorted Lists | Hard | LC |
Two Heaps Pattern
Problems where you split data into two halves — a max-heap for the lower half and a min-heap for the upper half. This lets you access the median or balance point in O(1).
| Problem | Difficulty | Link | |
|---|---|---|---|
| 295. Find Median from Data Stream | Hard | LC | |
| 480. Sliding Window Median | Hard | LC | |
| 502. IPO (Maximize Capital) | Hard | LC |
Minimum Number Pattern
Problems asking for the minimum cost, time, or number of operations. A min-heap lets you always greedily pick the cheapest option next.