FastPrepFree Windows Between Busy Intervals

Free Windows Between Busy Intervals

Scale AI logoScale AI● MediumNEW GRADOAPHONE SCREEN
Learn

Problem statement

Given occupied time intervals busy, return every positive-length free interval strictly between the earliest occupied start and the latest occupied end.

For this exercise, assume intervals use integer timestamps and are half-open: [start,end). Overlapping or touching occupied intervals form one occupied block. Time before the first occupied block or after the last occupied block is excluded. With fewer than two disjoint occupied blocks, return an empty array.

Return the gaps as [start,end] rows in increasing start order. The supplied schedules have already been read from their input interface; no network calls are required.

Function

freeWindows(busy: int[][]) → int[][]

Examples

Example 1

busy = [[4,7],[1,3],[9,12]]return = [[3,4],[7,9]]

The occupied blocks are [1,3), [4,7), and [9,12). Their two internal gaps are returned.

Example 2

busy = [[1,5],[2,3],[5,8],[10,11]]return = [[8,10]]

The nested and touching intervals combine into [1,8). The remaining gap is [8,10).

Example 3

busy = []return = []

No occupied blocks means there are no bounded internal gaps.

Constraints

  • For this exercise, assume 0 <= busy.length <= 100000.
  • Each row has two integers with 0 <= start < end <= 10^9.
  • The input may be unsorted and may contain duplicate or nested intervals.

More Scale AI problems

See Scale AI hiring insights
public int[][] freeWindows(int[][] busy) {
    // Write your solution here
}
busy[[4,7],[1,3],[9,12]]
expected[[3,4],[7,9]]
Checking account…