File: smallest-index-with-equal-value/solution.py

Date: 2026-06-06

Time: 19:09

Purpose

This file solves LeetCode 2057 — Smallest Index With Equal Value. It owns the single responsibility of finding the smallest index i in a list where i % 10 == nums[i].

Key Components

smallest_index(nums: list[int]) -> int

The sole public function. Contract:

Patterns

Linear scan with early exit — the simplest possible approach for a "find first match" problem. No preprocessing, no data structures. enumerate gives both index and value in a single pass, and the function returns immediately on the first hit. This is idiomatic Python for problems where you need the first element satisfying a predicate.

The % 10 operation extracts the ones digit of the index, which is the only digit that matters since nums[i] is constrained to [0, 9]. For indices 0–9, i % 10 == i. For indices 10+, only the last digit of the index is compared.

Dependencies

Imports: None — pure standard library Python.

Imported by: The "Imported By" list in the prompt is misleading — those are test files for *other* problems that happen to share a common test harness import pattern. The actual consumer is smallest-index-with-equal-value/test_solution.py.

Flow

1. Iterate over nums with enumerate, yielding (i, val) pairs starting from index 0.

2. For each pair, check if i % 10 == val.

3. On the first match, return i immediately.

4. If the loop exhausts without a match, return -1.

Time complexity: O(n) worst case, O(1) best case (match at index 0). Space complexity: O(1).

Invariants

Error Handling

None. The function trusts its caller to provide a valid list[int]. An empty list produces -1 (the for loop simply doesn't execute). No exceptions are raised or caught.

Topics to Explore

Beliefs