Heap / Priority Queue
This is a premium interview-preparation course on the **heap** — the data structure that keeps the smallest (or largest) element always one **O(log n)** pop away, without paying to keep everything sorted. In interviews it hides behind a family of recognisable phrases: **k-th largest**, **top k**, **k closest**, **merge k lists**, **schedule to minimise time**, and **running median**. The moment you hear one of those, a **priority queue** is usually the answer. This course starts with the mechanics — how a binary heap is stored in an array, how sift-up and sift-down maintain the heap property, and why building a heap is O(n) rather than O(n log n) — then works through curated problems grouped by the patterns interviewers actually test: fixed-size top-k heaps, k-way merges, greedy scheduling, the two-heap median trick, and advanced greedy-with-a-heap. Every lesson visualises the heap as both a tree and its backing array, dry-runs a concrete sequence of pushes and pops, and shows a clean Java 17 solution built on PriorityQueue.
19
Curated lessons
6
Learning modules
7h 16m
Est. study time
9
Target companies
What you'll learn
- Recognise the top-k, k-way-merge, scheduling, and median patterns and reach for a heap.
- Choose a min-heap vs a max-heap (and a fixed size k) to hit the target complexity.
- Explain the array layout, sift-up/sift-down, and why build-heap is O(n).
- Balance two heaps to answer streaming-median queries in O(log n) per element.
Your progress
0 / 19
problems completed
19 available now · keep going!
Mark lessons complete as you work through them. Progress is saved on this device and syncs to your account when you sign in.
Learning roadmap
Work top to bottom — each module builds on the last.
- 1
Heap Fundamentals
Keep the extreme element at the rootHow a binary heap works: min vs max heaps, the priority queue abstraction, and the sift/heapify operations that build one in O(n).
5/5 problems40m - 2
Top K Pattern
Size-k heap; evict the worst as you scanThe workhorse pattern: hold a fixed-size heap of k elements so the k-th best is always at the root.
4/4 problems1h 27m - 3
Merge Pattern
Heap of one candidate per listMerge k sorted sequences by always pulling the smallest current head from a heap of size k.
2/2 problems1h 10m - 4
Scheduling Pattern
Heap orders events or tasks by priorityGreedy scheduling where a heap tracks the next resource to free up or the most urgent task to run.
3/3 problems1h 15m - 5
Median Pattern
Two heaps split around the middleBalance a max-heap of the lower half against a min-heap of the upper half so the median sits at the two roots.
2/2 problems1h 10m - 6
Advanced Heap
Greedy choice maintained by a heapGreedy problems where a heap turns an exponential search into a series of locally optimal O(log n) choices.
3/3 problems1h 34m
Modules
6 themed modules from fundamentals to advanced.
Heap Fundamentals
How a binary heap works: min vs max heaps, the priority queue abstraction, and the sift/heapify operations that build one in O(n).
Top K Pattern
The workhorse pattern: hold a fixed-size heap of k elements so the k-th best is always at the root.
Merge Pattern
Merge k sorted sequences by always pulling the smallest current head from a heap of size k.
Scheduling Pattern
Greedy scheduling where a heap tracks the next resource to free up or the most urgent task to run.
Median Pattern
Balance a max-heap of the lower half against a min-heap of the upper half so the median sits at the two roots.
Advanced Heap
Greedy problems where a heap turns an exponential search into a series of locally optimal O(log n) choices.
Full curriculum
14 problems and 5 concept lessons in learning order.
- 1Min HeapHeap Fundamentals Concept
- 2Max HeapHeap Fundamentals Concept
- 3Priority QueueHeap Fundamentals Concept
- 4HeapifyHeap Fundamentals Concept
- 5Build HeapHeap Fundamentals Concept
- 6Kth Largest Element in an ArrayTop K PatternMedium
- 7Top K Frequent ElementsTop K PatternMedium
- 8K Closest Points to OriginTop K PatternMedium
- 9Find K Closest ElementsTop K PatternMedium
- 10Merge k Sorted ListsMerge PatternHard
- 11Smallest Range Covering Elements from K ListsMerge PatternHard
- 12Meeting Rooms IIScheduling PatternMedium
- 13Task SchedulerScheduling PatternMedium
- 14Single-Threaded CPUScheduling PatternMedium
- 15Find Median from Data StreamMedian PatternHard
- 16Sliding Window MedianMedian PatternHard
- 17IPOAdvanced HeapHard
- 18Furthest Building You Can ReachAdvanced HeapMedium
- 19Minimum Cost to Hire K WorkersAdvanced HeapHard