Interval List Intersections

You are given two lists of closed intervals, firstList and secondList. Each list is pairwise disjoint and sorted. Return the intersection of the two interval lists: every closed interval [lo, hi] of points that lie in some interval of both lists, in sorted order.

30:00

Interval List Intersections
medium
Free written solution
Topics
Companies

You are given two lists of closed intervals, firstList and secondList. Each list is pairwise disjoint and sorted. Return the intersection of the two interval lists: every closed interval [lo, hi] of points that lie in some interval of both lists, in sorted order.

Example 1:
Input: {"firstList":[[0,2],[5,10],[13,23],[24,25]],"secondList":[[1,5],[8,12],[15,24],[25,26]]}
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Constraints:
  • 0≤firstList.length,secondList.length≤10000 \leq \text{firstList.length}, \text{secondList.length} \leq 1000

  • firstList.length+secondList.length≥1\text{firstList.length} + \text{secondList.length} \geq 1

  • 0≤starti<endi≤1090 \leq start_i < end_i \leq 10^9

  • endi<starti+1end_i < start_{i+1} within each list

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"firstList":[[0,2],[5,10],[13,23],[24,25]],"secondList":[[1,5],[8,12],[15,24],[25,26]]}

Start with an empty answer. firstList has 4 intervals and secondList has 4.

number line

A

i=0

0,2

5,10

13,23

24,25

B

j=0

1,5

8,12

15,24

25,26

A∩B

0

26

lo = max(starts)

-

hi = min(ends)

-

lo <= hi ?

-

advance

-

answer (0)

[]

Variables
VariableValue
lenA4
lenB4
DepthFunction Call
Stack empty
0/23