Heap and priority queue in Python
Built-in heapq
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.
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
Turn this concept into a clear answer.
Practice this conceptAdapted from 18_DSA_PYTHON.md · “8. Heap and priority queue in Python”. Examples are study fragments; framework behavior can depend on your version.