File: intersection-of-three-sorted-arrays/solution.py

Date: 2026-06-06

Time: 17:06

Purpose

This file solves LeetCode 1213 — Intersection of Three Sorted Arrays. It finds all integers that appear in all three input arrays, which are each sorted in strictly increasing order. It belongs to the repo's collection of LeetCode solutions, each isolated in its own directory with a standard layout (solution.py, test_solution.py, plan.md, review.md).

Key Components

Solution.arraysIntersection

Signature: (arr1: List[int], arr2: List[int], arr3: List[int]) -> List[int]

Three-pointer merge that walks all three arrays simultaneously in a single pass. Returns a sorted list of values present in every array.

Patterns

Three-pointer technique. This is the canonical pattern for intersecting sorted sequences without extra space. Instead of using sets (O(n) space) or binary search (O(n log n)), it exploits the sorted invariant to advance pointers in lock-step — always advancing the pointer that points to the smallest value. When all three point to the same value, that value is in the intersection.

The branching logic at lines 20–26 implements this:

1. All equal → record the value, advance all three pointers.

2. arr1[i] is the global min → advance i (it can't match anything yet).

3. arr2[j] <= arr3[k]arr2[j] is the smallest of the remaining two, advance j.

4. Otherwisearr3[k] is smallest, advance k.

This is O(n₁ + n₂ + n₃) time, O(1) auxiliary space (ignoring the output list).

Dependencies

Imports: Only typing.List — no external or internal dependencies.

Imported by: intersection-of-three-sorted-arrays/test_solution.py directly. The massive "Imported By" list in the prompt is an artifact of the repo's test scaffolding — those other test files don't actually import this solution; they share a common test runner or import pattern.

Flow

1. Initialize three index variables i, j, k to 0.

2. Loop while all three indices are in bounds.

3. Compare the current elements at all three pointers.

4. If all match, append to result and advance all three.

5. Otherwise, advance the pointer pointing to the smallest element — this can never be part of a three-way match at its current position because at least one other array's current element is larger.

6. Return result once any array is exhausted.

Invariants

Error Handling

None. The function assumes valid input per the LeetCode contract (non-empty sorted arrays of integers). Empty arrays are handled implicitly — the while-loop condition short-circuits and returns [].

Topics to Explore

Beliefs