File: binary-search/solution.py

Date: 2026-06-06

Time: 15:24

binary-search/solution.py

Purpose

This implements LeetCode 704 — Binary Search, the canonical binary search on a sorted array. It's one of ~400+ solutions in the leetcode-implementations repo, each following the same convention: a Solution class with one public method matching LeetCode's expected signature.

Key Components

Solution.search(nums, target) -> int — The only method. Given a sorted array of unique integers and a target value, returns the index of the target or -1 if absent.

Patterns

Overflow-safe midpoint: mid = left + (right - left) // 2 instead of (left + right) // 2. In Python this doesn't matter (arbitrary-precision ints), but it's the standard idiom borrowed from C/Java where left + right can overflow. The repo consistently uses this form across its binary search variants.

Closed-interval search: Both left and right are inclusive bounds (left, right = 0, len(nums) - 1), and the loop runs while left <= right. This is the classic formulation where every iteration either finds the target or shrinks the search space by at least one element.

Three-way branch: The if/elif/else partitions the comparison into equal, less-than, and greater-than — clean and easy to reason about for an exact-match search.

Dependencies

Imports: None. The solution is self-contained with no standard library or third-party imports.

Imported by: The "Imported By" list in the prompt is misleading — those ~400 test files aren't importing *this* solution. They're importing their own solution.py via a shared test harness pattern. The actual consumer is binary-search/test_solution.py.

Flow

1. Initialize left = 0, right = len(nums) - 1 — the entire array.

2. Loop while left <= right (at least one candidate remains):

3. If the loop exits, the target doesn't exist — return -1.

Each iteration halves the search space, giving O(log n) time and O(1) space.

Invariants

Error Handling

None. An empty array (len(nums) == 0) is handled implicitly: right = -1, the while left <= right condition is immediately false, and -1 is returned. No exceptions are raised or caught.

Topics to Explore

Beliefs