Skip to content
Decoder

Interval List Intersections

Medium

Problem

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

Example 1

first  = [[0, 2], [5, 10], [13, 23], [24, 25]]

Example 2

second = [[1, 5], [8, 12], [15, 24], [25, 26]]

Example 3

  →  [[1, 2], [5, 5], [8, 10], [15, 23], [24, 24], [25, 25]]

Example 4

Input
first = [[1, 3], [5, 9]], second = []
Output
[]

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.