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.

ProblemDifficultyLink
373. Find K Pairs with Smallest SumsMediumLC
378. Kth Smallest Element in a Sorted MatrixMediumLC
23. Merge K Sorted ListsHardLC

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).

ProblemDifficultyLink
295. Find Median from Data StreamHardLC
480. Sliding Window MedianHardLC
502. IPO (Maximize Capital)HardLC

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.

ProblemDifficultyLink
1167. Minimum Cost to Connect SticksMediumLC
253. Meeting Rooms IIMediumLC
621. Task SchedulerMediumLC
759. Employee Free TimeHardLC
857. Minimum Cost to Hire K WorkersHardLC
871. Minimum Number of Refueling StopsHardLC