Heap patterns for coding interviews: how to recognize and solve heap problems

Most Heap problems in coding interviews can be grouped into four common patterns. Recognizing these patterns helps us quickly identify when to use a Heap or Priority Queue and how to approach the problem efficiently.

 

Top K Elements

We should use this pattern when we need to find the K largest, K smallest, K most frequent, or K closest elements. Instead of sorting the entire collection, maintain a Heap of size K containing only the best candidates found so far.
We need to use Min Heap because among the current K largest candidates, the smallest one is the first candidate we want to discard when a better element appears.

 

What to keep in your head before an interview:
-- K largest -> Min Heap of size K
-- K smallest -> Max Heap of size K

 

The heap keeps the worst element among the current Top K candidates at the top, so it can be removed efficiently when a better candidate appears.

 

Recognition clues:

 

-- Find the K largest/smallest elements
-- Find the Kth largest/smallest element
-- Find the K most frequent elements
-- Find the K closest points/elements
-- K is much smaller than the number of elements

 

The Core idea of this pattern:

for each element:
    push element into Min Heap

    if heap.size() > K:
        remove the smallest element

LeetCode examples:

 

215. Kth Largest Element in an Array
347. Top K Frequent Elements
692. Top K Frequent Words
703. Kth Largest Element in a Stream
973. K Closest Points to Origin

 

K-way Merge

The K-way Merge pattern is used when you have multiple sorted arrays, lists, or sequences and need to process their elements in sorted order.
Instead of merging all sequences and sorting everything again, keep the smallest current element from each sequence in a Min Heap.

 

At each step:

 

-- Remove the smallest element from the heap
-- Add it to the result or process it
-- Find the next smallest element across several sorted sequences
-- Find the Kth smallest element across sorted sequences
-- Each source can provide its next candidate after the current one is processed

 

The heap contains at most K elements - one candidate from each sorted sequence

 

Recognition clues:

 

-- Multiple sorted arrays or lists
-- Merge K sorted sequences
-- Find an element among several sorted sequences
-- Need the globally smallest next element

 

LeetCode examples:

 

23. Merge k Sorted Lists
373. Find K Pairs with Smallest Sums
632. Smallest Range Covering Elements from K Lists

 

Two Heaps

 

We should use this pattern when elements need to be divided into two halves while efficiently accessing the largest element of the lower half and the smallest element of the upper half.
The heaps are kept balanced so that their sizes differ by at most one element. This gives efficient access to the middle of the data, which makes the pattern very useful for median problems.

 

Typical:
-- a Max Heap for the smaller half
-- a Min Heap for the larger half

Lower half        Upper half
 Max Heap          Min Heap

1  3  5  7    |    9  12  15
         ↑         ↑

This is especially useful for median problems.

 

Recognition clues:

 

-- Find the median dynamically
-- Numbers arrive as a stream
-- Maintain two balanced halves
-- Need quick access to the middle element(s)

 

LeetCode examples:

 

295. Find Median from Data Stream
480. Sliding Window Median

 

Repeated Min / Max

We should use this pattern when we need to repeatedly access and remove the smallest or largest element from a changing collection.
After extracting the current minimum or maximum, the problem usually requires you to process it, modify it, combine it with another element, or insert a new value back into the collection.
A Heap is a natural fit because it provides efficient access to the current minimum or maximum without sorting the entire collection after every operation.

 

What you should keep in mind before an interview:
-- If the problem repeatedly asks for the current smallest or largest element while the collection is changing, think Heap / Priority Queue.

 

Typical process:

Heap
 ↓
extract min/max
 ↓
process element(s)
 ↓
insert new value
 ↓
repeat

 

Recognition clues:

 

-- Repeatedly take the smallest or largest element
-- Combine the smallest or largest elements
-- Remove the current minimum or maximum
-- Modify an element and insert it back
-- Repeat the operation until a condition is satisfied

 

LeetCode examples:

 

1046. Last Stone Weight
1962. Remove Stones to Minimize the Total
2558. Take Gifts From the Richest Pile
3066. Minimum Operations to Exceed Threshold Value II

 

What you should remember about Heap patterns before an interview:

 

Top K keeps only the best K candidates
K-way Merge coordinates multiple sorted sources
Two Heaps maintains two balanced halves
Repeated Min/Max repeatedly processes the current extreme element

© 2026 Algobytes