FunctionRacerProblemsRace

Overlaps of Two Schedules

MediumTwo pointersPython, JavaScript, TypeScript, Java, C++

first and second are two schedules. Each is a list of busy slots [start, end] with start <= end, sorted by start, and within one schedule the slots never overlap or touch: every slot ends strictly before the next one starts. Return every time range during which both schedules are busy, as a list of [start, end] pairs sorted by start.

Two slots overlap when they share at least one point: [2, 5] and [5, 7] share the single point 5, which gives [5, 5]. Return an empty list if the schedules never overlap. Do not change the inputs.

Example 1

first [[0,2],[5,10],[13,23],[24,25]]
second [[1,5],[8,12],[15,24],[25,26]]
returns [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]

[0, 2] meets [1, 5] on [1, 2]; [5, 10] meets [1, 5] on the point 5 and [8, 12] on [8, 10]; [13, 23] meets [15, 24] on [15, 23]; [24, 25] meets [15, 24] on 24 and [25, 26] on 25.

Example 2

first [[1,3],[5,9]]
second []
returns []

The second schedule has no slots, so nothing can overlap.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems