Skip to content

Interval List Intersections

MediumIntervals
Decode it first. Clues, constraints, pattern and plan before any code.

You get two lists of closed intervals, first and second. Each list is sorted and its own intervals are pairwise disjoint. Return the intersection of the two lists: every stretch covered by an interval from both lists, in sorted order. Closed means the ends are included, so [1, 5] and [5, 8] intersect at [5, 5].

first  = [[0, 2], [5, 10], [13, 23], [24, 25]]
second = [[1, 5], [8, 12], [15, 24], [25, 26]]
  →  [[1, 2], [5, 5], [8, 10], [15, 23], [24, 24], [25, 25]]
 
first = [[1, 3], [5, 9]], second = []  →  []

Constraints

  • 0 <= first.length, second.length <= 1000
  • 0 <= start < end <= 10^9
  • Within each list, every interval ends before the next one starts.