← All topics

Lesson

Heaps & Priority Queues

Get min/max in O(1), insert/pop in O(log n) — Top-K and scheduling.

Medium → HardTime O(n log k)Space O(k)
Practice problems
Loading visualization…

Loading solution…

Top-K trick

A min-heap of size k stores the k largest: root is the threshold. Replace when a larger candidate arrives.

Studio

Heap Top-K visualizes sift-up / sift-down as elements stream in.

Pitfalls

  • !Wrong heap polarity
  • !Comparing objects without a key
  • !Forgetting ties

Practice from this topic

Showing 24 of the related problems in the 1000-question bank.

View all →

Got the lesson?

Mark Heaps & Priority Queues as understood when the ideas stick.