MediumArrayTwo PointersIntervals

Interval List Intersections

LeetCode
1 approach, code in all languages

You are handed two lists of closed intervals, each list individually sorted by start value and internally free of overlaps. Compute the intersection of the two lists: every span that lies inside an interval from the first list and simultaneously inside an interval from the second list.

Return these overlapping spans as a sorted list of closed intervals. A shared single point counts as a valid intersection.

Example 1

Input: firstList = [[0,2],[5,10]], secondList = [[1,5],[8,12]]

Output: [[1,2],[5,5],[8,10]]

0-2 meets 1-5 at 1-2, 5-10 meets 1-5 at the single point 5, and 5-10 meets 8-12 at 8-10.

Example 2

Input: firstList = [[1,4]], secondList = [[5,7]]

Output: []

The only interval in each list sits entirely apart from the other, so there is no overlap.

Constraints

  • 0 ≤ firstList.length, secondList.length ≤ 1000
  • firstList.length + secondList.length ≥ 1
  • 0 ≤ start ≤ end ≤ 10^9
  • Each list is sorted by start and contains no overlapping intervals
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now