Interval List Intersections

Medium#986two lists

You get two lists of intervals. Each is sorted, and inside one list no two intervals overlap. Return every common part of an interval from the first list and an interval from the second, in order. A common part can be a single point. Our example: A = [1, 3], [5, 9], [11, 12], B = [0, 2], [4, 6], [8, 12] — the answer is [1, 2], [5, 6], [8, 9], [11, 12].

input
firstList, secondList: two lists of intervals [start, end]. Each list is sorted, and inside one list no two intervals overlap or touch. A list may be empty.
returns
Every common part of an interval of firstList and an interval of secondList, as [start, end], in order. A common part can be a single point, like [3, 3] of [1, 3] and [3, 5].
constraints
  • 0 ≤ firstList.length, secondList.length ≤ 1000
  • 1 ≤ firstList.length + secondList.length
  • 0 ≤ startᵢ < endᵢ ≤ 10⁹, and endᵢ < startᵢ₊₁ in each list
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[][]} firstList
 * @param {number[][]} secondList
 * @return {number[][]}
 */
function intervalIntersection(firstList, secondList) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more intervals problems

Pattern “Intervals” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.