What you will learn
Why a heap is a complete binary tree with the parent-child ordering property
The array layout where parent = (i-1)/2, left = 2i+1, and right = 2i+2
Insert, extract, peek, delete, and Floyd's build-heap, plus the up-heapify and down-heapify primitives behind them
The top-k pattern: a k-sized min-heap as a sliding window in O(N log K) time
The comparator pattern: custom ordering for user-defined types and inverted heaps
A median finder capstone that runs two heaps in tandem in O(log N) per insertion
Why this course
Most engineers can call heapq.heappush or PriorityQueue.add without ever explaining what happens after the push. Then an interviewer asks why Floyd's build-heap runs in O(N) when N consecutive inserts cost O(N log N), and the answer disappears.
This course rebuilds heaps from the complete-binary-tree-in-an-array layout up. You see why the heap property gives O(log N) insert and extract, why build-heap is amortised O(N), and how the same structure powers the top-k pattern, custom comparators, and a running median finder.
Requirements
The course assumes you can read code in any mainstream language and have seen a binary tree before, but no prior heap experience is needed.
- Comfort writing loops, conditionals, and classes in at least one language.
- Familiarity with binary trees and Big-O notation (O(log N), O(N log N)).
- A willingness to think in array indices and parent-child arithmetic.
Overview
A heap is a complete binary tree squeezed into a contiguous array. That sentence does almost all the work. Once you accept that the tree lives in an array indexed so the parent of i sits at (i-1)/2, the rest of the data structure (heap property, up-heapify, down-heapify, O(log N) insert and extract) follows from how a complete tree behaves on top of contiguous memory.
Heaps are the engine inside priority queues. Python's heapq, Java's PriorityQueue, C++'s std::priority_queue, and JavaScript's datastructures-js PriorityQueue are all heaps under the hood. Job schedulers, event simulations, Dijkstra's shortest path, A* search, and any "give me the highest-priority thing next" workload rests on this one structure.
Representation of a heap
This course covers both halves of the problem: how a heap works at the array level, and how to wield it on the two patterns that account for almost every heap question in interviews.
Fundamentals
The course opens by motivating the priority queue. You walk through how a linked list scan to find the maximum priority is O(N) per operation, why that breaks at scale, and how the priority queue abstract data type fills the gap. Then heaps enter as the most common priority queue implementation.
From there the course pins down the heap definition. A heap is a complete binary tree that obeys the heap ordering property (each node outranks its children). You see why "complete" keeps the height at O(log N), why max-heap and min-heap are just two orderings of the same shape, and which standard library exposes which by default.
The middle of the course is the array implementation. You see how to map a complete binary tree onto a contiguous array using the parent-child index math, how up-heapify and down-heapify restore the heap property in O(log N), and how insert, delete, peek, and extract all reduce to those two primitives. The section closes with Floyd's build-heap, which down-heapifies every non-leaf node from the bottom up and converts an unordered array into a heap in O(N) rather than the O(N log N) you would pay for N consecutive inserts.
Problem solving
After the fundamentals, the rest of the course is patterns. Almost every heap problem you see in an interview reduces to one of two templates, with a design layer on top where you assemble a heap yourself.
Each pattern is taught in three layers. An Understanding lesson explains the technique on a problem where the template is obvious. An Identifying lesson teaches the cues, phrases like "k largest", "k smallest", "k most frequent", or "ordered by a derived key", that flag a new problem as belonging to the pattern. Then four to five problems give progressively harder applications. The course ends with a design section where you build a max-heap and a min-heap from scratch and use both together to design a median finder that tracks the running median of a data stream in O(log N) per insertion.
How this course is different
Most heap material online either skims the array layout or stops at heapsort and never circles back to priority queues. This course does both properly.
(i-1)/2, 2i+1, and 2i+2 are obvious rather than magic.Who this course is for
The course suits anyone who needs to understand heaps as a data structure rather than treat them as a black-box priority queue call.
- New programmers who have seen trees and arrays but never how the two combine in a single data structure.
- Self-taught and bootcamp graduates who can solve "kth largest" with a sort but stall when asked for the O(N log K) heap version.
- Working developers who call
heapqorPriorityQueueregularly but cannot explain what happens inside on a push or pop. - Engineers preparing for FAANG and Big Tech interviews where top-k, k-way merge, and median-of-stream questions show up across onsite loops.
- Returning engineers who learned heapsort years ago and want a refresher rooted in priority queues and the modern interview pattern set.
- Anyone who wants to understand why Dijkstra's shortest path needs a heap and what falls apart without one.
Continue your DSA journey
Heaps connect outward to several other topics in this curriculum. The natural next steps depend on whether you want to deepen the data structure layer or push into algorithms that use the heap.
- Array: A heap is a complete binary tree implemented inside a contiguous array. The memory layout, index arithmetic, and amortised resize behaviour from the array course are the substrate every heap operation runs on top of.
- Hash table: Many top-k and comparator problems combine a heap with a hash table. You count frequencies in a hash table, then take the top k by frequency through a heap. The hash table course teaches the counting half of that pairing.
- Queue: A priority queue and a regular queue look similar from the outside but serve elements in entirely different orders. The queue course contrasts FIFO, LIFO, and priority semantics in one place, which clarifies why a heap is the right implementation only for the priority case.
- Binary tree: Heaps are a specialisation of binary trees that trade arbitrary shape for completeness and the heap-ordering property. Seeing the general structure first makes it clear which binary-tree mechanics still apply to heaps and which ones (in-order traversal, balanced rotations) do not.
- Graph: Dijkstra's shortest path algorithm is the canonical heap user. The graph course implements it on top of a priority queue, and this course explains what that priority queue is doing on every push and pop.
- Sorting: Heapsort is one of the sorting algorithms covered in the sorting course, and it falls out of the heap operations directly. Knowing both ends of the connection makes it harder for either course to feel like magic.
Frequently asked questions
std::priority_queue, PriorityQueue, heapq, datastructures-js) shown side by side. The concepts themselves (complete binary tree layout, up-heapify, down-heapify, Floyd build-heap) are language-agnostic.