Remove Interval - Problem

Imagine you're managing a calendar booking system where time slots are represented as intervals. You have a collection of non-overlapping time slots that are currently booked, and you need to cancel one specific booking that might overlap with multiple existing slots.

Given a sorted list of disjoint intervals intervals where each interval [a, b) represents a half-open interval (includes a but excludes b), and another interval toBeRemoved, return the resulting intervals after removing the specified interval.

Key Points:

  • Intervals are half-open: [a, b) includes a but excludes b
  • Input intervals are already sorted and non-overlapping
  • The removal might split existing intervals or completely remove them
  • Return the final set as sorted disjoint intervals

Input & Output

example_1.py โ€” Basic Removal
$ Input: intervals = [[0,2],[3,4],[5,7]], toBeRemoved = [1,6]
โ€บ Output: [[0,1],[6,7]]
๐Ÿ’ก Note: The removal interval [1,6) partially overlaps with [0,2) and [5,7). From [0,2), we keep [0,1). From [5,7), we keep [6,7). The interval [3,4) is completely contained in [1,6) so it's removed entirely.
example_2.py โ€” Complete Removal
$ Input: intervals = [[0,5]], toBeRemoved = [2,3]
โ€บ Output: [[0,2],[3,5]]
๐Ÿ’ก Note: The interval [0,5) is split by removing [2,3). This creates two new intervals: [0,2) and [3,5), both of which are outside the removed region.
example_3.py โ€” No Overlap
$ Input: intervals = [[0,5]], toBeRemoved = [5,6]
โ€บ Output: [[0,5]]
๐Ÿ’ก Note: Since intervals use half-open notation [a,b), the interval [0,5) ends at 5 (exclusive) and [5,6) starts at 5 (inclusive). There's no overlap, so the original interval is preserved.

Constraints

  • 1 โ‰ค intervals.length โ‰ค 104
  • intervals[i].length == 2
  • 0 โ‰ค ai < bi โ‰ค 109
  • intervals is sorted by ai in ascending order
  • toBeRemoved.length == 2
  • 0 โ‰ค toBeRemoved[0] < toBeRemoved[1] โ‰ค 109

Visualization

Tap to expand
๐ŸŽญ Theater Seat Reservation CancellationExisting Reservations:Seats 0-2Seats 3-4Seats 5-7Cancel Seats 1-6Processing Impact:โœ‚๏ธ [0,2) โ†’ Keep [0,1)โŒ [3,4) โ†’ Removedโœ‚๏ธ [5,7) โ†’ Keep [6,7)Final Reservations:Seats 0-1Seats 6-7๐Ÿ’ก Algorithm InsightsKey Principles:โ€ข Process reservations in sequenceโ€ข Check overlap with cancellationโ€ข Split partial overlaps smartlyโ€ข Maintain sorted order naturallyComplexity:โ€ข Time: O(n) - single passโ€ข Space: O(n) - output storage๐ŸŽฏ Optimal Solution!
Understanding the Visualization
1
Identify Affected Bookings
Scan through existing reservations to find those that overlap with the cancellation
2
Handle Partial Overlaps
For bookings that partially overlap, split them to preserve non-cancelled portions
3
Remove Complete Overlaps
Entirely remove bookings that fall completely within the cancellation
4
Maintain Order
Ensure the final reservation list remains sorted and non-overlapping
Key Takeaway
๐ŸŽฏ Key Insight: Since intervals are pre-sorted and disjoint, we can process them linearly, making intelligent decisions about splits and removals in a single pass - achieving optimal O(n) performance.
Asked in
Google 42 Amazon 38 Meta 24 Microsoft 19
28.6K Views
Medium-High Frequency
~18 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen