Roadmap › Stage 7: Heaps & Backtracking › Pattern 31 of 53
DSA pattern guide

Heap — Top K Elements

k largest/smallest/most frequent, k closest, merge k sorted lists.

Stage 7 · Heaps & Backtracking9 problems2 Easy · 4 Medium · 3 Hard

Use it when

k largest/smallest/most frequent, k closest, merge k sorted lists.

Key idea

Keep a heap of size k so each push costs O(log k). Use a min-heap for the largest k.

Practice problems, in order

  1. Kth Largest Element in a StreamEasy
  2. Last Stone WeightEasy
  3. Top K Frequent ElementsMedium
  4. K Frequent WordsMedium
  5. Kth Largest Element in an ArrayMedium
  6. Find K Pairs with Smallest SumsMedium
  7. Find Median from Data StreamHard
  8. Merge K Sorted ListsHard
  9. Smallest Range Covering K ListsHard

How to practise this pattern

  1. Read the problem and look for the signal above before writing any code.
  2. Say the brute force out loud, with its time complexity, then ask what work it repeats.
  3. Apply the key idea and write the optimised version yourself.
  4. If you're stuck for more than 10 minutes, take one small hint, not the full solution.
  5. Move to the next problem only when you can explain why this pattern fits.

Stuck on one of these?

The AlgoSolveo extension (free, launching soon) sits beside the LeetCode problem and gives the smallest hint that gets you moving, never the full answer. Solved problems tick off on your roadmap.

Join the extension waitlistTrack it on the roadmap
← Previous patternBinary Search TreeNext pattern →Heap — Sliding & Design