Compile Ready
DSA
DSA/Heap / Priority Queue

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%

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. 1

    Heap Fundamentals

    Keep the extreme element at the root

    How 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. 2

    Top K Pattern

    Size-k heap; evict the worst as you scan

    The 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. 3

    Merge Pattern

    Heap of one candidate per list

    Merge k sorted sequences by always pulling the smallest current head from a heap of size k.

    2/2 problems1h 10m
  4. 4

    Scheduling Pattern

    Heap orders events or tasks by priority

    Greedy scheduling where a heap tracks the next resource to free up or the most urgent task to run.

    3/3 problems1h 15m
  5. 5

    Median Pattern

    Two heaps split around the middle

    Balance 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. 6

    Advanced Heap

    Greedy choice maintained by a heap

    Greedy 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.

Full curriculum

14 problems and 5 concept lessons in learning order.

Techniques you'll master

ArrayArray LayoutBinary SearchBucket SortBuild HeapCapital AllocationComparatorComplexityCountingData StreamDesignGeometryGreedyHash MapHeapHeapifyIntervalsInvariantJavaK-Way MergeLazy DeletionLinked ListMathMax HeapMerge SortMin HeapPriority QueueQuickselectResource AllocationSchedulingSift DownSimulationSliding WindowSortingTop KTwo Heaps