LESSON 69 / 75·2 min readIntermediate

Heap and priority queue in Python

Should knowConcept lesson
01 Understand02 Recall03 Prepare

Built-in heapq

Python
import heapq

heap = [5, 3, 9]
heapq.heapify(heap)
heapq.heappush(heap, 1)
print(heap[0])

Problems to practice

  • Kth largest element
  • Top K frequent elements
  • Merge K sorted lists
  • Median in stream

Interview guidance

heapq gives us a min-heap by default. For max-heap, push negatives.


PAUSE & RECALL

Can you explain it without the notes?

Heap and priority queue in Python

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

Check your explanation

Built-in heapq

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 in Python

Adapted from 18_DSA_PYTHON.md · “8. Heap and priority queue in Python”. Examples are study fragments; framework behavior can depend on your version.