File: find-the-array-concatenation-value/solution.py

Date: 2026-06-06

Time: 16:44

Purpose

This file implements LeetCode 2562 — Find the Array Concatenation Value. It's a self-contained solution module following the repo's standard layout: a single function implementing the algorithm, plus inline unit tests. Its responsibility is solving exactly one problem: compute the sum of "concatenation values" formed by pairing elements from the front and back of the array, working inward.

Key Components

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

The core solver. Takes a 0-indexed integer array and returns the total concatenation value.

Contract: nums is non-empty (per problem constraints: 1 <= nums.length <= 1000). Each element is a positive integer. The function is pure — no side effects, no mutation of the input.

TestConcatenationValue

Six test cases covering both LeetCode examples, odd/even length arrays, boundary values (single element, large numbers), and a manual arithmetic check.

Patterns

Two-pointer inward sweep: The classic l/r converging pointer pattern. This is the natural fit — the problem literally defines the operation as pairing nums[0] with nums[n-1], then nums[1] with nums[n-2], etc.

String-based digit concatenation: Rather than computing nums[l] * 10^(digits of nums[r]) + nums[r] arithmetically, it converts both to strings, concatenates, and parses back. This is idiomatic for "concatenation value" problems — simpler and avoids manually counting digits.

Inline tests: The repo co-locates tests with solutions (this file doubles as the test module via unittest.main()). The separate test_solution.py imports from here.

Dependencies

Imports: Only unittest from the standard library. No external dependencies — typical for LeetCode solutions.

Imported by: The testsolution.py in this same directory, plus the massive list of other test files suggests a shared test infrastructure that cross-references solutions (likely the runtests.py at the repo root discovers and runs all test modules).

Flow

1. Initialize result = 0 and two pointers l = 0, r = len(nums) - 1.

2. While l < r: concatenate the string representations of nums[l] and nums[r], parse to int, add to result. Advance both pointers inward.

3. If the array has odd length, l == r after the loop — add the unpaired middle element directly.

4. Return the accumulated sum.

For [7, 52, 2, 4]: pair 7||4 = 74, pair 52||2 = 522, total = 596.

Invariants

Error Handling

None. The function assumes valid input per LeetCode constraints. No bounds checking, no empty-array guard. An empty list would return 0 (the loop and if both skip), which is arguably correct but not explicitly tested.