What is Heap / Priority Queue

Pattern overview

heap (priority queue with O(logn)O(\log n) operations) maintains elements with efficient access to the minimum or maximum, supporting insert and extract in O(logn)O(\log n). You use it when repeatedly finding the smallest or largest element from a changing collection. Heaps enable O(nlogn)O(n \log n) sorting and O(nlogk)O(n \log k) selection of top k elements.

You'll see this in scheduling, merging, and streaming data problems. If you can explain these concepts in your own words, you'll solve related problems faster under interview pressure.

1770079577822-on2j90m6vus.jpg