mission-peace/interview · error · IllegalArgumentException

Not same length input

Error message

Not same length input

What it means

longestSpan computes the longest span of equal sums between two binary arrays using prefix-sum differences. It requires both arrays to have identical length; if they differ it throws IllegalArgumentException("Not same length input") immediately, before any computation, because the span definition is meaningless for unequal lengths.

Solutions

  1. Verify both arrays have the same length before calling and fix the data source so sizes match
  2. Pad or truncate the longer array to the shorter length if a partial comparison is intended
  3. Wrap the call in try/catch for IllegalArgumentException and surface a clear message to the caller

Example fix

// before
int span = algo.longestSpan(arr1, arr2);
// after
if (arr1.length != arr2.length) {
    throw new IllegalArgumentException("arrays must match: " + arr1.length + " vs " + arr2.length);
}
int span = algo.longestSpan(arr1, arr2);
Defensive patterns

Strategy: validation

Validate before calling

if (input1 == null || input2 == null || input1.length != input2.length) {
    throw new IllegalArgumentException("inputs must be non-null and same length");
}

Try / catch

try {
    int span = algo.longestSpan(a, b);
} catch (IllegalArgumentException e) {
    log.warn("length mismatch: {}", e.getMessage());
}

Prevention

When it happens

Trigger: Calling longestSpan(a, b) where a.length != b.length, e.g. passing data collected from two differently-sized sources or after one array was truncated/extended.

Common situations: Off-by-one slicing of arrays, comparing today's binary sequence with a differently-sized historical one, or misaligned test fixtures where one array gained/lost elements during refactoring.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of mission-peace/interview@94be5deb0c (2026-09-08). Data as JSON: /api/errors/5183da8b908d8166. Report an issue: GitHub.

Appendix: source

Thrown at src/com/interview/array/LongestSameSumSpan.java:22

import java.util.Map;

/**
 * Date 12/29/2015
 * @author Tushar Roy
 *
 * Give two arrays of same size consisting of 0s and 1s find span (i, j) such that
 * sum of input1[i..j] = sum of input2[i..j]
 *
 * Time complexity O(n)
 * Space complexity O(n)
 *
 * http://www.geeksforgeeks.org/longest-span-sum-two-binary-arrays/
 */
public class LongestSameSumSpan {

    public int longestSpan(int input1[], int input2[]) {
        if (input1.length != input2.length) {
            throw new IllegalArgumentException("Not same length input");
        }
        Map<Integer, Integer> diff = new HashMap<>();
        int prefix1 = 0, prefix2 = 0;
        int maxSpan = 0;
        diff.put(0, -1);
        for (int i = 0; i < input1.length ; i++) {
            prefix1 += input1[i];
            prefix2 += input2[i];
            int currDiff = prefix1 - prefix2;
            if (diff.containsKey(currDiff)) {
                maxSpan = Math.max(maxSpan, i - diff.get(currDiff));
            } else {
                diff.put(currDiff, i);
            }
        }
        return maxSpan;
    }

View on GitHub (pinned to 94be5deb0c)