File: intersection-of-multiple-arrays/solution.py

Date: 2026-06-06

Time: 17:05

Purpose

This file solves LeetCode 2248: Intersection of Multiple Arrays. It finds all integers that appear in every sub-array of a 2D list and returns them sorted. It's a standalone solution module following the repo's convention of one problem per directory with solution.py, test_solution.py, plan.md, and review.md.

Key Components

intersection(nums: List[List[int]]) -> List[int] — The sole public function. Takes a list of lists of distinct positive integers, returns a sorted list of integers common to all sub-arrays.

Patterns

Set-intersection reduction: Seeds result with set(nums[0]), then iteratively intersects (&=) with each remaining sub-array. This is the canonical Python idiom for multi-set intersection — equivalent to set.intersection(*map(set, nums)) but written as an explicit loop.

Separate-then-sort: Computes the unordered result via sets, then applies sorted() at the end. This cleanly separates the membership logic from the ordering requirement.

Dependencies

Flow

1. Convert nums[0] to a set → result

2. For each subsequent array in nums[1:], intersect result with set(arr) in-place

3. Sort the remaining elements and return

The set shrinks monotonically on each iteration — elements can only be removed, never added.

Invariants

Error Handling

None. The function trusts its input matches the LeetCode contract. An empty nums would crash on line result = set(nums[0]). This is appropriate — the problem guarantees at least one sub-array.

Topics to Explore

Beliefs