{"record":{"id":"4860f9f1df303744","repo":"TheAlgorithms/Java","slug":"x-and-y-arrays-must-have-the-same-length","errorCode":null,"errorMessage":"x and y arrays must have the same length.","messagePattern":"x and y arrays must have the same length\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/Neville.java","lineNumber":35,"sourceCode":"public final class Neville {\n\n    private Neville() {\n    }\n\n    /**\n     * Evaluates the polynomial that passes through the given points at a\n     * specific x-coordinate.\n     *\n     * @param x The x-coordinates of the points. Must be the same length as y.\n     * @param y The y-coordinates of the points. Must be the same length as x.\n     * @param target The x-coordinate at which to evaluate the polynomial.\n     * @return The interpolated y-value at the target x-coordinate.\n     * @throws IllegalArgumentException if the lengths of x and y arrays are\n     * different, if the arrays are empty, or if x-coordinates are not unique.\n     */\n    public static double interpolate(double[] x, double[] y, double target) {\n        if (x.length != y.length) {\n            throw new IllegalArgumentException(\"x and y arrays must have the same length.\");\n        }\n        if (x.length == 0) {\n            throw new IllegalArgumentException(\"Input arrays cannot be empty.\");\n        }\n\n        // Check for duplicate x-coordinates to prevent division by zero\n        Set<Double> seenX = new HashSet<>();\n        for (double val : x) {\n            if (!seenX.add(val)) {\n                throw new IllegalArgumentException(\"Input x-coordinates must be unique.\");\n            }\n        }\n\n        int n = x.length;\n        double[] p = new double[n];\n        System.arraycopy(y, 0, p, 0, n); // Initialize p with y values\n\n        for (int k = 1; k < n; k++) {","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/Neville.java#L17-L53","documentation":"Thrown by Neville.interpolate(double[] x, double[] y, double target) when the x and y arrays have different lengths. Neville's algorithm interpolates a polynomial through (x, y) points; mismatched arrays represent an inconsistent point set. This check runs before the emptiness check, so length mismatch is detected first.","triggerScenarios":"Calling interpolate with x.length != y.length, e.g., interpolate(new double[]{1,2,3}, new double[]{1,2}, 1.5). Common when x and y come from different data sources or when one array is truncated.","commonSituations":"Reading x and y coordinates from separate columns or API responses where one has missing entries. Copy-paste errors in test data. Off-by-one in array slicing that affects only one array.","solutions":["Validate that x.length == y.length before calling interpolate","Ensure data ingestion produces paired (x, y) entries atomically rather than separate arrays","Add a unit test that verifies array-length equality for interpolation inputs"],"exampleFix":"// before\ndouble result = Neville.interpolate(xCoords, yCoords, target);\n\n// after\nif (xCoords.length != yCoords.length) {\n    throw new IllegalArgumentException(\"x and y must have equal length\");\n}\ndouble result = Neville.interpolate(xCoords, yCoords, target);","handlingStrategy":"validation","validationCode":"if (x.length != y.length) {\n    throw new IllegalArgumentException(\"x and y arrays must have equal length\");\n}\ndouble result = Neville.interpolate(x, y, target);","typeGuard":"static boolean arePairedArrays(double[] x, double[] y) {\n    return x != null && y != null && x.length == y.length;\n}","tryCatchPattern":"try {\n    double result = Neville.interpolate(x, y, target);\n} catch (IllegalArgumentException e) {\n    // x and y length mismatch — check data pipeline\n    logger.error(\"Interpolation input mismatch: x.length={}, y.length={}\", x.length, y.length);\n}","preventionTips":["Pair x and y data at ingestion time rather than building separate arrays","Add a precondition assertion before interpolation calls","Unit test with mismatched arrays to confirm the guard fires correctly"],"tags":["math","validation","illegal-argument","interpolation","array-length"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}