{"record":{"id":"33afbff5ebcc459c","repo":"TheAlgorithms/Java","slug":"input-n-must-be-non-negative","errorCode":null,"errorMessage":"Input n must be non-negative","messagePattern":"Input n must be non-negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java","lineNumber":36,"sourceCode":" * </ul>\n * * @author Varun Upadhyay (https://github.com/varunu28)\n */\npublic final class Fibonacci {\n    private Fibonacci() {\n    }\n\n    static final Map<Integer, Integer> CACHE = new HashMap<>();\n\n    /**\n     * This method finds the nth fibonacci number using memoization technique\n     *\n     * @param n The input n for which we have to determine the fibonacci number\n     * Outputs the nth fibonacci number\n     * @throws IllegalArgumentException if n is negative\n     */\n    public static int fibMemo(int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Input n must be non-negative\");\n        }\n        if (CACHE.containsKey(n)) {\n            return CACHE.get(n);\n        }\n\n        int f;\n\n        if (n <= 1) {\n            f = n;\n        } else {\n            f = fibMemo(n - 1) + fibMemo(n - 2);\n            CACHE.put(n, f);\n        }\n        return f;\n    }\n\n    /**\n     * This method finds the nth fibonacci number using bottom up","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java#L18-L54","documentation":"fibMemo(int n) computes the nth Fibonacci number using top-down memoization. Negative indices have no mathematical meaning in this sequence, so n < 0 is rejected with IllegalArgumentException. The method caches results in a static HashMap for subsequent calls.","triggerScenarios":"Calling Fibonacci.fibMemo(-1) or any negative n, often from user input parsed without bounds checking or from an off-by-one loop that produces a negative index.","commonSituations":"Parsing user-supplied indices without validation; array index arithmetic that underflows; incorrect decrement in a loop feeding n.","solutions":["Validate n >= 0 before calling fibMemo.","Clamp the input: use Math.max(0, n) if a default of fib(0)=0 is acceptable.","Review the calling code's loop bounds and index arithmetic."],"exampleFix":"// before\nint r = Fibonacci.fibMemo(userInput); // throws if userInput < 0\n\n// after\nif (n < 0) throw new IllegalArgumentException(\"Index must be >= 0\");\nint r = Fibonacci.fibMemo(n);","handlingStrategy":"validation","validationCode":"if (n < 0) {\n    throw new IllegalArgumentException(\"Index must be non-negative, got: \" + n);\n}\nint result = Fibonacci.fibMemo(n);","typeGuard":"static boolean isValidFibIndex(int n) {\n    return n >= 0;\n}","tryCatchPattern":"try {\n    result = Fibonacci.fibMemo(n);\n} catch (IllegalArgumentException e) {\n    result = 0; // default for invalid index\n}","preventionTips":["Validate user-supplied indices at the input boundary.","Use Math.max(0, n) when a zero default is acceptable."],"tags":["fibonacci","negative-input","input-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}