← Questions

Find the gaps between intervals

0
NuroScale AICodingLast asked Apr 2026

A coding interview question asked in Scale AI's coding round and at Nuro, last asked Apr 2026. It tests intervals and sorting.

You are given a list of integer intervals [start, end], inclusive, in any order. They may overlap or touch. Return every maximal range of integers between the smallest and the largest covered integer that no interval covers. It is merge intervals, but you return the spaces in between.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • find_gaps(intervals: list[list[int]]) -> list[list[int]] returns the gaps as inclusive [gap_start, gap_end] pairs, sorted.
  • [1, 3] and [4, 6] leave no gap: no integer between them is missing.

Example

find_gaps([[1, 3], [7, 10], [2, 4]])                 ->  [[5, 6]]       # union is [1, 4] and [7, 10]
find_gaps([[1, 3], [4, 6]])                          ->  []             # touching integers
find_gaps([[5, 5], [1, 2]])                          ->  [[3, 4]]
find_gaps([[1, 10], [2, 3], [12, 12], [15, 20]])     ->  [[11, 11], [13, 14]]

Constraints

  • 1 <= len(intervals) <= 2 · 10^5
  • Endpoints fit in 32-bit signed integers; start <= end.

Clarifying questions

  • Are intervals inclusive, and do touching intervals leave a gap?
  • Are gaps only between the first start and the last end, or also outside them?
  • Is the input sorted?

What a strong answer covers

  • Sort by start, sweep while tracking the furthest end covered so far; when the next start is greater than end + 1, emit [end + 1, start - 1].
  • O(n log n) time for the sort, O(1) extra beyond the output.
  • Intervals contained in others, duplicates, a single interval.
  • Overflow care with end + 1 at the 32-bit limit in typed languages.
  • Tests with the examples.

Follow-ups

  • Find the gaps within a given window [lo, hi], including before the first and after the last interval.
  • Intervals arrive one at a time; maintain the gaps.
  • Return the length of the largest gap only, using O(1) extra space after sorting.

Where it's asked

  • Scale AI · Coding · Last asked Apr 2026Live coding, one or two rounds, Python
  • Nuro · Technical phone screen · Last asked Nov 2025

What it tests

intervals · sorting

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding