Quick 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.

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

  1. 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.
  2. Overlapping or touching obstacles together form a single blocked stretch. Combine them before you read off the free time between stretches.
  3. A free interval needs strictly positive length, so two obstacles that touch, such as [1, 3] and [3, 5], leave nothing between them.

Free Intervals Inside an Observation Window

mediumHudson River TradingSoftware Engineer

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

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

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

Examples

Example 1
Input
([[1, 3], [5, 7], [9, 12]], 0, 15)
Output
[[0, 1], [3, 5], [7, 9], [12, 15]]
Notes
Source example 1: free before the first obstacle, in each gap between obstacles, and after the last one up to 15.
Example 2
Input
([[0, 5], [3, 8], [10, 12]], 0, 12)
Output
[[8, 10]]
Notes
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.

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

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def find_free_intervals(intervals: list[list[int]], left: int, right: int) -> list[list[int]]:
Sample call
find_free_intervals([[1, 3], [5, 7], [9, 12]], 0, 15)
Language
Case 1
Input
([[1, 3], [5, 7], [9, 12]], 0, 15)
Expected
[[0, 1], [3, 5], [7, 9], [12, 15]]
Case 2
Input
([[0, 5], [3, 8], [10, 12]], 0, 12)
Expected
[[8, 10]]

Browse More Questions

Show the approach

Approach

Clip every obstacle to the window: [s, e] becomes [max(s, left), min(e, right)], and the piece is dropped when its clipped start is not below its clipped end. This removes obstacles that lie outside the window or only touch one of its edges. Sort the remaining pieces by start and sweep them with a value cur that begins at left. Invariant: every moment in [left, cur] has been either covered by a piece already seen or reported as free. For a piece [s, e], if s > cur, nothing covers the time from cur to s, since every earlier piece ends at or before cur and every later piece starts at or after s, so [cur, s] is reported. Then cur becomes max(cur, e); the maximum is needed because a nested piece can end before the current blocked stretch does. After the sweep, [cur, right] is reported if cur < right. A gap is emitted only when s > cur strictly, so touching pieces never produce a zero-length interval. Two reported gaps are always separated by a piece of positive length, so each gap is maximal, and gaps come out in increasing order. Edge cases: a piece that covers the whole window gives an empty list, and when every obstacle is clipped away the answer is the single interval [left, right].

Time complexity:
O(n log n)
Space complexity:
O(n)