> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Interval List Intersections Python Solution

> Tested Python solution for LeetCode 986 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 986, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), Sweep Line. [View on LeetCode](https://leetcode.com/problems/interval-list-intersections/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 986   # by problem number
lcpy gen -s interval_list_intersections   # by problem name
```

## Problem

You are given two lists of closed intervals, `firstList` and `secondList`, where `firstList[i] = [starti, endi]` and `secondList[j] = [startj, endj]`. Each list of intervals is pairwise disjoint and in sorted order.

Return *the intersection of these two interval lists*.

A closed interval `[a, b]` (with `a <= b`) denotes the set of real numbers `x` with `a <= x <= b`.

The **intersection** of two closed intervals is a set of real numbers that are either empty or represented as a closed interval. For example, the intersection of `[1, 3]` and `[2, 4]` is `[2, 3]`.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2019/01/30/interval1.png)

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

```
Input: firstList = [[1,3],[5,9]], secondList = []
Output: []
```

### Constraints

* `0 <= firstList.length, secondList.length <= 1000`
* `firstList.length + secondList.length >= 1`
* `0 <= starti < endi <= 10^9`
* `endi < starti+1`
* `0 <= startj < endj <= 10^9`
* `endj < startj+1`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/interval_list_intersections/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/interval_list_intersections/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m + n)
    # Space: O(1) excluding output
    def interval_intersection(
        self, first_list: list[list[int]], second_list: list[list[int]]
    ) -> list[list[int]]:
        result: list[list[int]] = []
        i = j = 0
        while i < len(first_list) and j < len(second_list):
            lo = max(first_list[i][0], second_list[j][0])
            hi = min(first_list[i][1], second_list[j][1])
            if lo <= hi:
                result.append([lo, hi])
            if first_list[i][1] < second_list[j][1]:
                i += 1
            else:
                j += 1
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(m + n) | O(1) excluding output |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.