What is Heap / Priority Queue

Pattern overview

heap (priority queue with O(log⁡n)O(\log n) operations) maintains elements with efficient access to the minimum or maximum, supporting insert and extract in O(log⁡n)O(\log n). You use it when repeatedly finding the smallest or largest element from a changing collection. Heaps enable O(nlog⁡n)O(n \log n) sorting and O(nlog⁡k)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