Heap / Priority Queue Interview Questions & Solutions
11 solutions ยท 1 Easy ยท 9 Medium ยท 1 Hard
A heap is the right tool whenever you repeatedly need the current minimum or maximum of a changing set. Instead of re-sorting, a priority queue hands you the extreme element in O(log n) and lets you push new ones just as cheaply. Interviewers use heap problems to check whether you recognize that "top k," "k-th largest," and "merge many sorted things" all share this structure.
These solutions cover the classics: top-k frequent elements, k-th largest, merging k sorted lists, and task-scheduling problems where you always want to process the most urgent item next. You'll also see the elegant two-heaps trick for maintaining a running median, and the key efficiency insight that a size-k heap solves "top k" in O(n log k) rather than sorting everything in O(n log n). We're explicit about choosing a min-heap versus a max-heap โ getting that backwards is the most common bug here โ and about when a heap genuinely beats sorting. Recognize the "k" and "running" keywords and you'll know to reach for a priority queue.
- Last Stone WeightEasy
- Furthest Building You Can ReachMedium
- K Closest Points to OriginMedium
- K-th Smallest Prime FractionMedium
- Kth Largest Element in an ArrayMedium
- Remove Stones to Minimize the TotalMedium
- Reorganize StringMedium
- Top K Frequent ElementsMedium
- Top K Frequent WordsMedium
- Total Cost to Hire K WorkersMedium
- Smallest Range Covering Elements from K ListsHard
Practice Heap / Priority Queue the right way
SkillFlow serves you Heap / Priority Queue problems matched to your level, times you like a real interview, and tracks your proficiency as it climbs. Free.
Start practicing free โ