Skip to content

Heap / Priority Queue

A priority queue is a waiting line where the most important person is always at the front, no matter when they arrived. A heap gives you the min (or max) in O(1) and inserts/removes in O(log n) — perfect for "K largest", "next smallest" and "keep the best so far".

After this topic: You can pick min-heap vs max-heap, keep a heap of size k, and combine two heaps for medians.

Do these first: Trees

0 of 7 solved0%

Step 1 · Read the lesson

Heap / Top-K

Keep only the best K items; the heap tells you the worst of them in O(log k).

Step 2 · Solve the problems in order

Try each one for about 20 minutes first. Problems with a Run code tab are checked right here. If you are stuck, open Nudge, think again, then Idea. Go skeleton only gives the function shape, and Reference solution is for comparing after you have tried. Tick the box when you could solve it again without help.

  1. 1.Kth Largest Element in a StreamEasy
  2. 2.Last Stone WeightEasy
  3. 3.K Closest Points to OriginMedium
  4. 4.Kth Largest Element in an ArrayMedium
  5. 5.Task SchedulerMedium
  6. 6.Design TwitterMedium
  7. 7.Find Median from Data StreamHard

Step 3 · What this unlocks