{"record":{"id":"5183da8b908d8166","repo":"mission-peace/interview","slug":"not-same-length-input","errorCode":null,"errorMessage":"Not same length input","messagePattern":"Not same length input","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/interview/array/LongestSameSumSpan.java","lineNumber":22,"sourceCode":"import java.util.Map;\n\n/**\n * Date 12/29/2015\n * @author Tushar Roy\n *\n * Give two arrays of same size consisting of 0s and 1s find span (i, j) such that\n * sum of input1[i..j] = sum of input2[i..j]\n *\n * Time complexity O(n)\n * Space complexity O(n)\n *\n * http://www.geeksforgeeks.org/longest-span-sum-two-binary-arrays/\n */\npublic class LongestSameSumSpan {\n\n    public int longestSpan(int input1[], int input2[]) {\n        if (input1.length != input2.length) {\n            throw new IllegalArgumentException(\"Not same length input\");\n        }\n        Map<Integer, Integer> diff = new HashMap<>();\n        int prefix1 = 0, prefix2 = 0;\n        int maxSpan = 0;\n        diff.put(0, -1);\n        for (int i = 0; i < input1.length ; i++) {\n            prefix1 += input1[i];\n            prefix2 += input2[i];\n            int currDiff = prefix1 - prefix2;\n            if (diff.containsKey(currDiff)) {\n                maxSpan = Math.max(maxSpan, i - diff.get(currDiff));\n            } else {\n                diff.put(currDiff, i);\n            }\n        }\n        return maxSpan;\n    }\n","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/mission-peace/interview/blob/94be5deb0c0df30ade2a569cf3056b7cc1e012f4/src/com/interview/array/LongestSameSumSpan.java#L4-L40","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify both arrays have the same length before calling and fix the data source so sizes match","Pad or truncate the longer array to the shorter length if a partial comparison is intended","Wrap the call in try/catch for IllegalArgumentException and surface a clear message to the caller"],"exampleFix":"// before\nint span = algo.longestSpan(arr1, arr2);\n// after\nif (arr1.length != arr2.length) {\n    throw new IllegalArgumentException(\"arrays must match: \" + arr1.length + \" vs \" + arr2.length);\n}\nint span = algo.longestSpan(arr1, arr2);","handlingStrategy":"validation","validationCode":"if (input1 == null || input2 == null || input1.length != input2.length) {\n    throw new IllegalArgumentException(\"inputs must be non-null and same length\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    int span = algo.longestSpan(a, b);\n} catch (IllegalArgumentException e) {\n    log.warn(\"length mismatch: {}\", e.getMessage());\n}","preventionTips":["Assert equal lengths in tests for every longestSpan call","Slice both arrays from the same indexed source so lengths stay aligned","Validate array lengths at API/service boundaries before internal use"],"tags":["java","argument-validation","arrays"],"backgroundTag":"invalid-argument-value","analyzedSha":"94be5deb0c0df30ade2a569cf3056b7cc1e012f4","analyzedAt":"2026-09-08T13:27:05.954Z","contentChangedAt":"2026-09-08T13:27:05.954Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}