Algorithms from zero
Heaps & priority queues: interview drill
You understand the heap. Interviews test whether you can spot when “I only need the k best” beats sorting, reach for a priority queue under a timer, and explain the cost out loud.
Solve each problem before you reveal a hint, hit the target time, and narrate the time and space complexity as if an interviewer were listening. The hints exist for when you are genuinely stuck — they nudge you toward the pattern, never the full solution.
Five NeetCode-150 problems on the heap and priority-queue pattern this unit teaches. Set a timer, solve each cold without looking at a hint, then say the time and space complexity out loud before you move on. Reveal a hint only when you are truly stuck — the hints nudge, they never hand you the answer.
0/5 solved
heap priority queue
Follow-up (aloud)
Why a min-heap of size k rather than a max-heap of everything? Narrate the per-add complexity and how it depends on k, not n.
Follow-up (aloud)
Each round shrinks the heap by at least one. State the overall time complexity and why the heap operations dominate it.
Follow-up (aloud)
Quickselect averages O(n) but its worst case is O(n²). What input triggers the worst case, and how does a randomised pivot defend against it?
Follow-up (aloud)
Compare the size-k heap approach to a quickselect partition by distance. State each one's time complexity and when you'd pick which.
Follow-up (aloud)
Argue why the greedy 'always run the most frequent ready task' is optimal here. What would break if two tasks tie for the maximum count?
Mark each problem solved once you finished it cold, inside the target time, and could state the complexity without hesitation. Come back in a few days and re-solve the ones you marked — spaced revisits are what turn a recognised pattern into a reflex.