Skip to content

Glossary: Coding Interviews in JavaScript

Heap for top k

Also written: heap, min-heap, MinHeap, max-heap, top k

A heap keeps its smallest (or largest) item on top and costs O(log n) per push or pop. To find the k largest, keep a min-heap of only k items and pop the smallest whenever it grows past k, for O(n log k) time.

Where the book teaches it

Related terms

ComparatorPattern