Heap & Priority Queue Problems
Master the heap data structure and priority queue patterns that dominate coding interviews. Learn top-K selection, merge-K patterns, two-heap techniques, and scheduling problems — all with complete Python solutions using heapq. These patterns power ML beam search, priority scheduling in operating systems, and top-K recommendation engines.
Your Learning Path
Follow these lessons in order, or jump to any topic that interests you.
1. Heap Fundamentals
Min/max heap properties, Python heapq module, when to use heaps, and ML applications in beam search and top-K selection.
2. Top-K Elements
5 problems: kth largest element, top K frequent elements, K closest points to origin, sort characters by frequency, reorganize string.
3. Merge K Patterns
4 problems: merge K sorted lists, smallest range covering elements, kth smallest element in sorted matrix, find K pairs with smallest sums.
4. Two Heaps Pattern
4 problems: find median from data stream, sliding window median, IPO problem, maximize capital with limited selections.
5. Scheduling Problems
5 problems: task scheduler, meeting rooms II, CPU intervals, job scheduling with deadlines, process scheduling with priorities.
6. Advanced Heap Problems
4 hard problems: super ugly numbers, the skyline problem, trapping rain water II (3D), maximum performance of a team.
7. Patterns & Tips
Heap pattern cheat sheet, Python heapq tricks and gotchas, decision tree for choosing heap vs sorting, and an FAQ accordion.
What You'll Learn
By the end of this course, you will be able to:
Recognize Heap Patterns
Instantly identify when a problem requires a min-heap, max-heap, two-heap, or lazy deletion approach based on the problem constraints.
Master heapq in Python
Leverage Python's heapq module for efficient O(n log k) solutions including custom comparators, tuple priorities, and heap invariants.
Solve Scheduling & Merge
Tackle interval scheduling, K-way merge, and resource allocation problems that frequently appear in system design interviews.
Apply to ML Systems
Use heap patterns in beam search decoding, priority-based job scheduling, top-K recommendation, and streaming aggregation pipelines.
Lilly Tech Systems