Data Structures19 sections · 729 units
Open in Course

Insert Interval Solution

Implement your solution

Implement the three-phase algorithm. Your goal is to track which intervals overlap with the new one.

Two intervals overlap if one starts before the other ends AND ends after the other starts.

In phase 2, you expand the new interval to encompass all overlapping intervals, then add the expanded interval once. Time: O(n)O(n) single pass. Space: O(n)O(n) for output. Test cases:

  • New interval at start
  • New interval at end
  • New interval merges everything
  • New interval merges nothing