All puzzles › Heaps and Top-K

Heaps and Top-K Practice Problems

24 puzzles (10 easy, 13 medium, 1 hard), 3 free, in Python, JavaScript and C++.

A heap keeps the smallest or largest item ready to take at any moment, and puts a new item in its place in logarithmic time. It does not keep everything sorted, only enough order to always know the best one, which is exactly why it is fast. These problems are about the k best of something, merging sorted streams, and always doing the most urgent job next.

How to spot a heaps and top-k problem

Listen for top k, k closest, k most frequent, or anything described as a stream where you need the current best at every moment. If you find yourself re-sorting a list each time something is added, a heap does the same job for a fraction of the work.

New to it? Read the lesson first: Heaps & Priority Queues.

The puzzles, easiest first

  1. Find the Children (Easy): Given an index i in a heap stored as an array, and the heap's size, return [left child, right child].
  2. Find the Parent (Easy): Given an index i in a heap stored as an array, return its parent's index, or -1 for the root at index 0.
  3. Heap After Pops (Easy): Given a min-heap and a number n, pop the smallest value n times and return what's left, smallest first.
  4. Is It a Min-Heap? (Easy): Given an array, return true if it's a valid min-heap.
  5. Kth Largest (Easy): Given an array and k, return the kth largest value.
  6. Kth Smallest (Easy): Given an array and k, return the kth smallest value.
  7. Leaderboard Top K (Easy): Given a list of names, their scores, and k, return the names of the top k scorers, highest first.
  8. Peek the Minimum (Easy): Given a min-heap stored as an array, return its smallest value, or null if it's empty.
  9. Smallest Free Seat (Easy): Given a list of seat numbers that are already taken, return the lowest seat number that's free.
  10. Top K Largest (Easy): Given an array and k, return the k largest values, biggest first.
  11. Cut the Weakest Weights (Medium, free): Most of a trained network's weights are close to zero and can be removed with little harm.
  12. Highest Score Keeps the Key (Medium, free): To decide which server holds a key, score every server against that key and pick the highest.
  13. Keep the Best Beams (Medium, free): Beam search keeps several half-finished sentences alive instead of betting on one.
  14. Busiest Task Count (Medium): Given a list of tasks, return how many times the most common task appears.
  15. Cheapest Rope Join (Medium): Given a list of rope lengths, join them all into one rope for the lowest total cost.
  16. K Closest to a Target (Medium): Given an array, a target, and k, return the k values closest to the target, sorted from smallest to largest.
  17. Last Stone Standing (Medium): Given a pile of stone weights, keep smashing the two heaviest together.
  18. Lock On (Medium): Auto-aim locks on to the closest enemies.
  19. Median of a Window (Medium): Given a list of numbers, return the median, rounded to 2 decimals.
  20. Merge Sorted Piles (Medium): Given several sorted lists, merge them into one sorted list.
  21. Most Used Words (Medium): Given a list of words and k, return the k most common words.
  22. Sift Up (Medium): Given a min-heap and a new value, add the value and return the fixed heap.
  23. Top K Frequent (Medium): Given an array and k, return the k values that show up most often, most frequent first.
  24. Fewest Top-Ups (Hard): Given gas stations as [position, fuel], your starting fuel, and a target distance, return the fewest stops to reach the target, or -1 if you can't.

Practice another pattern

Or browse by subject

Browse all 676 puzzles