Begin Algo
Data structures · 3 lessons

Heap / Priority QueueAlways the largest or smallest, in O(log n)

A heap is a complete binary tree stored in an array, where a parent is always smaller (or larger) than its children. It does not sort everything — it only guarantees the top — which is why insert and extract are both logarithmic. It is the standard way to build a priority queue.

Why learn Heap / Priority Queue

Where it shows up
OS process scheduling

High-priority processes run first and new ones arrive at any time. A priority queue makes both "add" and "take the highest" fast; Linux's CFS uses a related structure.

→ Lesson: Binary Heap
Top 10 most-read articles

Ten million articles, and you want the ten most read. You do not need to sort them. Keep a min-heap of size 10 and make one pass.

→ Lesson: Top-K Problems
A running median

Data keeps arriving and you need the median at any moment. A max-heap holds the lower half, a min-heap the upper half, and the median sits at the two tops.

→ Lesson: Two Heaps

Lessons

3 lessons