If you need to repeatedly grab the minimum (or maximum) element, heaps are the data structure you reach for. They power priority queues, scheduling systems, and a surprising number of interview problems. This post breaks down what heaps are, how they work under the hood, and why their performance characteristics matter — especially for "top K" and "next best" problems.
What is a heap?
A heap is a complete binary tree where every parent is smaller than (or equal to) its children. This is called the min-heap property. The root is always the smallest element. Flip the rule — every parent is larger than its children — and you get a max-heap.