Remove Interval

MediumIntervalssimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A set of numbers is described by intervals, a list of half-open ranges [a, b) (containing every x with a <= x < b). The ranges are sorted by start and are strictly separated: each range ends before the next one begins (b_i < a_{i+1}). Remove the half-open range toBeRemoved = [x, y) from the set and return what is left in the same format: sorted, non-empty half-open ranges. Return an empty list if nothing remains.

Examples

Input: intervals = [[0,2],[3,4],[5,7]], toBeRemoved = [1,6]
Output: [[0,1],[6,7]]
Removing `[1,6)` cuts the tail off `[0,2)`, deletes `[3,4)`, and cuts the head off `[5,7)`.
Input: intervals = [[0,5]], toBeRemoved = [2,3]
Output: [[0,2],[3,5]]
The removed range sits strictly inside `[0,5)`, splitting it in two.
Input: intervals = [[-5,-4],[-3,-2],[1,2],[3,5],[8,9]], toBeRemoved = [-1,4]
Output: [[-5,-4],[-3,-2],[4,5],[8,9]]
Only `[1,2)` and part of `[3,5)` overlap `[-1,4)`.

Constraints

  • 1 <= intervals.length <= 10^4
  • -10^9 <= a_i < b_i <= 10^9
  • b_i < a_{i+1} for consecutive intervals
  • toBeRemoved.length == 2 and x < y

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems