Engrain by ByteRush

DSA.08.02 Mid-levelCore — asked often

Pattern 8: Intervals and greedy

Comes up in: DSA

This concept is being written. It gets the full five steps — explanation, predict, interview angle, practice with tests, and review cards. Here is exactly what it will cover, so you can study it elsewhere in the meantime.

What it covers

  • sort by start (or end)
  • merge rule
  • sweep line with events
  • meeting rooms with a heap
  • greedy choice property and exchange argument (intuition)

Words you will know

intervalsweep linegreedy algorithm

What you will practise

  • merge-intervals (M)
  • insert-interval (M)
  • non-overlapping-intervals (M)
  • meeting-rooms-ii (M)
  • jump-game (M)
  • gas-station (M)
  • max-subarray-kadane (M)

Interview questions this answers

  • Merge overlapping intervals. Why sort first?
  • How many meeting rooms are needed for a list of meetings?
  • How do you know a greedy algorithm is correct?