LESSON 09 / 75·2 min readIntermediate

Heap and priority queue

Must knowConcept lesson
01 Understand02 Recall03 Prepare

Overview

A heap is a complete binary tree that satisfies heap order. A priority queue uses it to always expose the highest or lowest priority element quickly.

Problems to practice

  • Kth largest element
  • Top K frequent elements
  • Merge K sorted lists
  • Find median in a stream
  • Smallest number in a range with priority

Time and space complexity

  • Insert: O(log n)
  • Extract min/max: O(log n)
  • Peek: O(1)

Example interview answer

“I would use a min-heap because we need the smallest k candidates constantly. This keeps the heap size bounded and gives efficient insertion and removal.”


PAUSE & RECALL

Can you explain it without the notes?

Heap and priority queue

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

What it is

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Graphs

Adapted from 14_DSA_MASTER.md · “9. Heap and priority queue”. Examples are study fragments; framework behavior can depend on your version.