{"record":{"id":"e1a8f0ae9dedd9ee","repo":"TheAlgorithms/Java","slug":"keys-and-frequencies-must-have-the-same-length","errorCode":null,"errorMessage":"Keys and frequencies must have the same length","messagePattern":"Keys and frequencies must have the same length","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/OptimalBinarySearchTree.java","lineNumber":92,"sourceCode":"                    long currentCost = frequencySum + leftCost + rightCost;\n\n                    if (currentCost < optimalCost[start][end]) {\n                        optimalCost[start][end] = currentCost;\n                        root[start][end] = currentRoot;\n                    }\n                }\n            }\n        }\n\n        return optimalCost[0][nodeCount - 1];\n    }\n\n    private static void validateInput(int[] keys, int[] frequencies) {\n        if (keys == null || frequencies == null) {\n            throw new IllegalArgumentException(\"Keys and frequencies cannot be null\");\n        }\n        if (keys.length != frequencies.length) {\n            throw new IllegalArgumentException(\"Keys and frequencies must have the same length\");\n        }\n\n        for (int frequency : frequencies) {\n            if (frequency < 0) {\n                throw new IllegalArgumentException(\"Frequencies cannot be negative\");\n            }\n        }\n    }\n\n    private static int[][] sortNodes(int[] keys, int[] frequencies) {\n        int[][] sortedNodes = new int[keys.length][2];\n        for (int index = 0; index < keys.length; index++) {\n            sortedNodes[index][0] = keys[index];\n            sortedNodes[index][1] = frequencies[index];\n        }\n\n        // Sort by key so the nodes can be treated as an in-order BST sequence.\n        Arrays.sort(sortedNodes, Comparator.comparingInt(node -> node[0]));","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/OptimalBinarySearchTree.java#L74-L110","documentation":"Thrown by OptimalBinarySearchTree.validateInput when keys.length != frequencies.length. Each key must be paired with exactly one frequency; mismatched lengths cause index-out-of-bounds later. Message: 'Keys and frequencies must have the same length'.","triggerScenarios":"Building keys and frequencies from two different data sources that drifted; removing a key without removing its frequency or vice versa; off-by-one when copying subarrays.","commonSituations":"Editing a hand-written test fixture; merging data from two queries that should be zipped but weren't; CSV import with a ragged row.","solutions":["Assert keys.length == frequencies.length at the point of construction and fail loudly there.","Generate both arrays from a single source (zip keys with frequencies) so they cannot diverge.","Add a defensive length check in tests/fixtures."],"exampleFix":"// before\nint[] keys = {10,20,30};\nint[] freq = {4,2}; // mismatched\nOptimalBinarySearchTree.optimize(keys, freq);\n\n// after\nif (keys.length != freq.length) {\n    throw new IllegalStateException(\"keys/freq length mismatch\");\n}\nOptimalBinarySearchTree.optimize(keys, freq);","handlingStrategy":"validation","validationCode":"if (keys.length != frequencies.length) {\n    throw new IllegalArgumentException(\"keys/frequencies length mismatch\");\n}\n// then call optimize(keys, frequencies)","typeGuard":"keys != null && frequencies != null && keys.length == frequencies.length","tryCatchPattern":null,"preventionTips":["Zip keys and frequencies from a single list of pairs so lengths stay equal.","Add an assertion at the data-build step.","Unit-test the equal-length invariant."],"tags":["input-validation","array","length-mismatch","dynamic-programming"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}