Find Time Intervals Without Obstacles
Company: Hudson River Trading
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
You are given a list of obstacle intervals `intervals`, where `intervals[i] = [start_i, end_i]` is a time interval during which an obstacle is present. You are also given two integers `left` and `right`, the start and end of an observation window.
Return a list of free time intervals within `[left, right]` during which no obstacle is present. Each returned interval has the form `[a, b]` with `a < b`, and the returned intervals must be sorted by start time, non-overlapping, and maximal (adjacent free segments are merged into one).
If there are no free intervals, return an empty list.
### Function Signature
```python
def find_free_intervals(intervals: list[list[int]], left: int, right: int) -> list[list[int]]:
```
### Rules
- An obstacle `[s, e]` blocks the time between `s` and `e`. A free interval may begin exactly where an obstacle ends and end exactly where one begins, so the obstacle `[1, 3]` in the window `[0, 5]` leaves `[0, 1]` and `[3, 5]` free.
- Obstacles can come in any order, and they can overlap, touch, or extend past the window. Only the part of an obstacle inside `[left, right]` matters.
- Obstacles that touch, such as `[1, 3]` and `[3, 5]`, leave no free time between them. Never return a zero-length interval.
- Return the free intervals sorted by start time.
### Constraints
- `1 <= len(intervals) <= 10^4`
- `0 <= start_i < end_i <= 10^9`
- `0 <= left < right <= 10^9`
### Examples
```text
Input: intervals = [[1,3],[5,7],[9,12]], left = 0, right = 15
Output: [[0,1],[3,5],[7,9],[12,15]]
```
The window is free from 0 until the first obstacle starts at 1, in each gap between consecutive obstacles, and from 12 to the end of the window at 15.
```text
Input: intervals = [[0,5],[3,8],[10,12]], left = 0, right = 12
Output: [[8,10]]
```
The obstacles `[0,5]` and `[3,8]` merge into `[0,8]`, and `[10,12]` runs to the end of the window, so the only free time is from 8 to 10.
```text
Input: intervals = [[2,4],[8,20],[4,6]], left = 3, right = 10
Output: [[6,8]]
```
Inside the window the obstacles become `[3,4]`, `[8,10]` and `[4,6]`. The pieces `[3,4]` and `[4,6]` touch and merge into `[3,6]`, so the window is blocked from 3 to 6 and from 8 to 10, which leaves `[6,8]`.
Overview: Given a list of obstacle time intervals and an observation window, return the maximal free intervals inside the window, sorted by start time. The problem tests sorting and merging overlapping or touching intervals, clipping ranges to a window, and handling the gaps at both edges.
You are given a list of obstacle intervals `intervals`, where `intervals[i] = [start_i, end_i]` is a time interval during which an obstacle is present. You are also given two integers `left` and `right`, the start and end of an observation window.
Return the list of free time intervals within `[left, right]` during which no obstacle is present. Each returned interval has the form `[a, b]` with `left <= a < b <= right`. The returned intervals must be sorted by start time, non-overlapping, and maximal, which means adjacent free segments are merged into one interval. If there are no free intervals, return an empty list.
### Rules
- An obstacle `[s, e]` blocks the time between `s` and `e`. A free interval may begin exactly where an obstacle ends and end exactly where one begins, so the obstacle `[1, 3]` in the window `[0, 5]` leaves `[0, 1]` and `[3, 5]` free.
- Obstacles can come in any order, and they can overlap (one may contain another or repeat it exactly), touch, or extend past the window. Only the part of an obstacle inside `[left, right]` matters.
- Obstacles that touch, such as `[1, 3]` and `[3, 5]`, leave no free time between them. Never return a zero-length interval.
- Return the free intervals sorted by start time.
Every value is at most 10^9, so it fits in a signed 32-bit integer (`int` in Java and C++).
### Example 1
```text
Input: intervals = [[1,3],[5,7],[9,12]], left = 0, right = 15
Output: [[0,1],[3,5],[7,9],[12,15]]
```
The window is free from 0 until the first obstacle starts at 1, in each gap between consecutive obstacles, and from 12 to the end of the window at 15.
### Example 2
```text
Input: intervals = [[2,4],[8,20],[4,6]], left = 3, right = 10
Output: [[6,8]]
```
Inside the window the obstacles become `[3,4]`, `[8,10]` and `[4,6]`. The pieces `[3,4]` and `[4,6]` touch and merge into `[3,6]`, so the window is blocked from 3 to 6 and from 8 to 10, which leaves `[6,8]`.
### Constraints
- `1 <= len(intervals) <= 10^4`
- `intervals[i] = [start_i, end_i]` holds exactly two integers
- `0 <= start_i < end_i <= 10^9`
- `0 <= left < right <= 10^9`
Constraints
- 1 <= len(intervals) <= 10^4
- intervals[i] = [start_i, end_i] holds exactly two integers
- 0 <= start_i < end_i <= 10^9
- 0 <= left < right <= 10^9
- All values fit in a signed 32-bit integer
Examples
Input: ([[1, 3], [5, 7], [9, 12]], 0, 15)
Expected Output: [[0, 1], [3, 5], [7, 9], [12, 15]]
Explanation: Source example 1: free before the first obstacle, in each gap between obstacles, and after the last one up to 15.
Input: ([[0, 5], [3, 8], [10, 12]], 0, 12)
Expected Output: [[8, 10]]
Explanation: Source example 2: [0,5] and [3,8] merge into [0,8] and [10,12] runs to the window end, so only [8,10] is free.
Hints
- Only the part of each obstacle inside [left, right] affects the answer. Decide what an obstacle that ends exactly at left, or starts exactly at right, contributes.
- Overlapping or touching obstacles together form a single blocked stretch. Combine them before you read off the free time between stretches.
- A free interval needs strictly positive length, so two obstacles that touch, such as [1, 3] and [3, 5], leave nothing between them.