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.

The 2 heap patterns you'll master
Top-k elements
A k-sized min-heap walked across the array as a sliding window that keeps the k largest items seen so far, answering any "aggregate over the top k" question in O(N log K) time and O(K) space. Kth Largest Element, Kth Smallest Element, K Range Sum, K Sorted Array Sorting.
Comparator
A custom ordering function that lets a heap prioritise user-defined types beyond primitive integer and character keys, with inversion to reuse a min-heap as a max-heap and stable tie-breaking on a secondary key. K Most Frequent Elements, K Smallest Sum Pairs, K Closest Values, K Arrays Smallest Range, K-Way List Merge.

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.

Most DSA resources
This course
Define a heap as "a tree that satisfies the heap property" and never explain how it lives in an array.
Derives the array layout from the complete binary tree so (i-1)/2, 2i+1, and 2i+2 are obvious rather than magic.
Teach heapsort once and treat the heap as a sorting trick rather than a priority queue.
Treats the heap as the priority queue it actually is and motivates every operation from real workloads.
Skip Floyd's build-heap, leaving you unable to explain why it runs in O(N) instead of O(N log N).
Proves why Floyd's build-heap is O(N) by counting work per level rather than per node.
Cover top-k problems one at a time so you memorise solutions without seeing the shared template.
Groups top-k problems into one pattern and teaches the identification signals before the solutions.
Stop at primitive integers and never show how a comparator orders user-defined types.
Covers comparators across C++, Java, Python, and JavaScript with inversion and stable tie-breaking.

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 heapq or PriorityQueue regularly 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

Code examples are given in C++, Java, Python, TypeScript, and JavaScript, with each language's native priority queue (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.
Yes, with one caveat. The course assumes you have already seen what a binary tree is. If you can read a recursive function and understand Big-O notation like O(log N), you have everything else you need. Arrays and binary trees are listed as comfortable prerequisites rather than hard ones.
There are around 16 lessons and 15 problems across 2 patterns plus a design section that builds a max-heap, a min-heap, and a median finder. Most learners finish the lessons in 6 to 10 hours and need another 10 to 15 hours to work through the problems, depending on prior experience.
The patterns covered (top-k elements and comparator-based ordering) and the design problems (max-heap, min-heap, median finder) cover a large share of the heap questions asked in real onsite loops. Kth-largest variants, k-way merge, k closest points, and median-of-data-stream all reduce to one of the templates taught here.
LeetCode shows you heap problems one at a time with no grouping and no derivation. This course teaches the heap from the complete-binary-tree-in-an-array layout up, proves why each operation has the complexity it does, then drills problems inside each of the two patterns so you build pattern recognition rather than memorising individual solutions.
The naive bound counts O(log N) work for every one of the N nodes and gets O(N log N). The tighter analysis counts work per level instead: leaves do zero sift-down work, the next level up does one swap, the one above does two, and so on. Half the nodes are leaves, a quarter are one level up, an eighth are two levels up. Summing that geometric series gives O(N) total work. The course walks through the proof and shows why it does not contradict the O(N log N) lower bound on heapsort, which still has to pay O(log N) per extraction.
A priority queue is an abstract data type. It promises that you can insert items with priorities and always retrieve the highest-priority item next. A heap is one concrete implementation of that promise and the most common one. You could also implement a priority queue with a sorted linked list, a balanced BST, or a Fibonacci heap, each with different complexity trade-offs. In most languages the standard-library "priority queue" type is a binary heap inside.
Yes. Once you finish all lessons and the design section, codeintuition issues a shareable certificate you can post to LinkedIn or attach to a job application.