File: minimum-common-value/solution.py

Date: 2026-06-06

Time: 17:54

Purpose

This file solves LeetCode 2540 — Minimum Common Value. It owns a single responsibility: given two sorted integer arrays, find the smallest integer that appears in both. It returns -1 if no common element exists.

Key Components

mincommonnumber(nums1, nums2) -> int

The sole public function. Contract:

Patterns

Two-pointer merge scan. This is the canonical pattern for finding common elements in two sorted sequences without extra space. Two indices advance through the arrays in lockstep: the pointer on the smaller value advances, because anything behind it can never match anything ahead in the other array. On equality, we return immediately — the first match is guaranteed to be the minimum because both arrays are scanned left to right.

This is the same merge logic used in merge sort's merge step, but short-circuited on the first match instead of producing a full merged output.

Dependencies

Imports: None — the solution is self-contained, using only built-in list and int.

Imported by: The testsolution.py in the same directory. The long "Imported By" list in the prompt is misleading — those are unrelated test files across the repo that happen to share a common test harness, not direct consumers of mincommon_number.

Flow

1. Initialize two pointers i = 0, j = 0.

2. While both pointers are in bounds:

3. If either pointer runs past the end, no common value exists → return -1.

Each iteration advances at least one pointer, so the loop terminates in at most len(nums1) + len(nums2) steps.

Invariants

Error Handling

None. The function assumes valid input (two sorted lists of integers). Empty lists are handled correctly — the while condition fails immediately and -1 is returned. There are no exceptions raised or caught.

Topics to Explore

Beliefs