InterviewLegend

Heaps, Intervals & Scheduling

Alex Chen40 min readUpdated 557 real interview reports

Anything phrased as "the k most frequent", "the k closest", or "merge these sorted streams" is a heap problem, whatever the wording around it suggests. The family shows up often enough at Google that it deserves its own home. A heap hands you the smallest (or largest) element in O(1) and reorders itself in O(log n) on each push or pop. That single property is what lets you answer top-k questions without paying to sort the entire input.

Two ideas cover almost everything here. The first is the size-k heap: keep only k elements on the heap and evict the worst one whenever a better candidate arrives, so you never sort all n items. The second is the k-way merge: load the front of every sorted source into one heap, pop the global minimum, and pull the next item from whichever source it came from. Learn those two and the variants are bookkeeping.

What's inside
  • 6 problems, worked step by step
  • 6 diagrams
  • about 40 min

Premium lessonGoogle interview guide

These took digging.

What candidates walked out with, from 20,000+ reports, forum threads behind logins and write-ups in Chinese. Then solved step by step.

100 companies. Checked every month.

Unlock all 100 companies

$49 a year · less than $1 a week · Cancel anytime

Had your Google interview?Tell us what they asked. Reviewed before it goes in, never with your name.
Google interview guide

Know what Google asks before you walk in.

34 coding · 7 system design · 47 behavioral questions

Get Premium$49 a year · Cancel anytime
Or start freeNo card.

Get their questions first.

InterviewLegend is independent and not affiliated with or endorsed by any company it covers.