Skip to main content
Heap Pattern

What is Heap?

Heap is a complete binary tree that maintains the heap property: parent is always smaller (min-heap) or larger (max-heap) than children. It provides O(log n) insertion and O(1) access to min/max.
Quick Recognition: If you need k largest/smallest, merge sorted lists, or continuously find min/max, think Heap!

Pattern Recognition Checklist

Use Heap When

  • Need k largest/smallest elements
  • Merge k sorted lists/arrays
  • Continuously track min/max
  • Priority-based processing
  • Median from data stream

Don't Use When

  • Need sorted order (use sort)
  • Need arbitrary access by index
  • Single min/max needed once
  • Memory is very constrained

When to Use

Top K Elements

Find k largest/smallest elements

Merge K Lists

Merge multiple sorted sequences

Scheduling

Process by priority, deadlines

Median Finding

Running median with two heaps

Pattern Variations

1. Kth Largest Element

import heapq

def find_kth_largest(nums, k):
    """Find kth largest using min-heap of size k"""
    min_heap = []
    
    for num in nums:
        heapq.heappush(min_heap, num)
        if len(min_heap) > k:
            heapq.heappop(min_heap)
    
    return min_heap[0]

# Alternative: Use nlargest
def find_kth_largest_alt(nums, k):
    return heapq.nlargest(k, nums)[-1]

2. Top K Frequent Elements

import heapq
from collections import Counter

def top_k_frequent(nums, k):
    """Find k most frequent elements"""
    count = Counter(nums)
    
    # Min-heap of (frequency, element)
    min_heap = []
    for num, freq in count.items():
        heapq.heappush(min_heap, (freq, num))
        if len(min_heap) > k:
            heapq.heappop(min_heap)
    
    return [num for freq, num in min_heap]

3. Merge K Sorted Lists

import heapq

def merge_k_lists(lists):
    """Merge k sorted linked lists"""
    min_heap = []
    
    # Add first element from each list
    for i, lst in enumerate(lists):
        if lst:
            heapq.heappush(min_heap, (lst.val, i, lst))
    
    dummy = ListNode(0)
    current = dummy
    
    while min_heap:
        val, i, node = heapq.heappop(min_heap)
        current.next = node
        current = current.next
        
        if node.next:
            heapq.heappush(min_heap, (node.next.val, i, node.next))
    
    return dummy.next

4. Find Median from Data Stream

import heapq

class MedianFinder:
    """Two heaps: max-heap for smaller half, min-heap for larger half"""
    
    def __init__(self):
        self.small = []  # Max-heap (negated values)
        self.large = []  # Min-heap
    
    def addNum(self, num):
        # Add to max-heap (small)
        heapq.heappush(self.small, -num)
        
        # Ensure max of small <= min of large
        if self.small and self.large and -self.small[0] > self.large[0]:
            val = -heapq.heappop(self.small)
            heapq.heappush(self.large, val)
        
        # Balance sizes (small can have at most 1 more)
        if len(self.small) > len(self.large) + 1:
            val = -heapq.heappop(self.small)
            heapq.heappush(self.large, val)
        if len(self.large) > len(self.small):
            val = heapq.heappop(self.large)
            heapq.heappush(self.small, -val)
    
    def findMedian(self):
        if len(self.small) > len(self.large):
            return -self.small[0]
        return (-self.small[0] + self.large[0]) / 2

5. K Closest Points to Origin

import heapq

def k_closest(points, k):
    """Find k closest points to origin"""
    # Max-heap of (-distance, point) to keep k smallest
    max_heap = []
    
    for x, y in points:
        dist = -(x * x + y * y)  # Negate for max-heap
        
        if len(max_heap) < k:
            heapq.heappush(max_heap, (dist, [x, y]))
        elif dist > max_heap[0][0]:
            heapq.heapreplace(max_heap, (dist, [x, y]))
    
    return [point for _, point in max_heap]

Classic Problems

Pattern: Min-heap of size kKey: Top of min-heap is kth largest after processing all
Pattern: Min-heap of list headsKey: Always pop smallest, push its next element
Pattern: Two heaps (max for small, min for large)Key: Keep heaps balanced, median from tops
Pattern: Max-heap for most frequent tasksKey: Process most frequent first with cooldown

Common Mistakes

Avoid These Pitfalls:
  1. Wrong heap type: Min-heap for k largest, max-heap for k smallest
  2. Python max-heap: Use negative values with heapq
  3. Heap size management: Pop when size exceeds k
  4. Custom comparators: Be careful with lambda syntax in each language

The Counter-Intuitive Heap Choice

This confuses many beginners:
ProblemUse This HeapWhy
K largest elementsMin-heap of size kTop is smallest of k largest
K smallest elementsMax-heap of size kTop is largest of k smallest
Stream medianBoth heapsMax for lower half, min for upper

Complexity Quick Reference

OperationTimeNote
InsertO(log n)Bubble up
Extract min/maxO(log n)Bubble down
Peek min/maxO(1)Root element
Build heapO(n)Heapify all
K largest from nO(n log k)Maintain size k

Interview Problems by Company

ProblemCompanyKey Concept
Kth Largest ElementAll FAANGMin-heap of size k
Top K FrequentAmazon, GoogleFrequency + heap
K Closest PointsMeta, AmazonMax-heap of size k
Sort Characters by FreqAmazonFrequency + max-heap
Task SchedulerMetaMax-heap + cooldown

Interview Tips

Script for interviews:
  1. “Since I need the k largest, I’ll use a min-heap of size k.”
  2. “I’ll iterate through all elements, pushing each to the heap.”
  3. “When heap size exceeds k, I pop the smallest.”
  4. “After processing all, the heap contains the k largest.”
  5. “Time is O(n log k), space is O(k).”
Interviewer SaysYou Should Think
”Find k largest/smallest”Heap of size k
”Merge k sorted arrays”Min-heap of heads
”Continuous median”Two heaps
”Process by priority”Priority queue
”Stream of numbers”Heap for dynamic data
Python’s heapq is a min-heap only. For max-heap:
import heapq

# Push negative values
heapq.heappush(heap, -val)

# Pop and negate
max_val = -heapq.heappop(heap)

# Peek
max_val = -heap[0]
For custom objects, use tuples: (priority, item)

Practice Problems

ProblemDifficultyLink
Kth Largest ElementMediumLeetCode 215
Top K Frequent ElementsMediumLeetCode 347
Merge K Sorted ListsHardLeetCode 23
Find Median from Data StreamHardLeetCode 295
K Closest Points to OriginMediumLeetCode 973

Practice Roadmap

1

Day 1: K Elements

  • Solve: Kth Largest Element, Top K Frequent
  • Focus: When to use min vs max heap
2

Day 2: Merging

  • Solve: Merge K Sorted Lists, K Pairs with Smallest Sum
  • Focus: Multi-source merging
3

Day 3: Two Heaps

  • Solve: Find Median from Data Stream
  • Focus: Balancing two heaps
4

Day 4: Applications

  • Solve: Task Scheduler, Meeting Rooms II
  • Focus: Scheduling with heaps
Interview Tip: For “k largest/smallest” problems, think heap. Use opposite heap type to maintain window of k elements.