Find Time Intervals Without Obstacles

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.

Hudson River Trading logo
Hudson River Trading
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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

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

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.

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.

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