Java Dynamic Programming Program · OT70
Nth Fibonacci Number with Memoization in Java
Plain recursion computes the same Fibonacci numbers again and again. Storing each result the first time it is computed turns exponential time into linear time.
Problem Statement
Return the n-th Fibonacci number, where fib(0) = 0, fib(1) = 1 and fib(n) = fib(n - 1) + fib(n - 2).
The result is a long, which holds exact values up to fib(92).
Input/Output Examples
n = 10Output55Explanation: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.n = 50Output12586269025Explanation: plain recursion would need billions of calls.n = 90Output2880067194370816120Explanation: still fits in a long.Visual Explanation
- Plain fib(5)callsfib(3) × 2fib(2) × 3repeated work 15 calls
- Memo fib(5)cache2345each value once
- Donen - 1 additionslinear time
fib(5) computes fib(3) twice and fib(2) three times (amber). With a cache, every value is computed once.The number of plain recursive calls grows like the Fibonacci numbers themselves. Memoization keeps one entry per n, so the work is linear.
Approaches
Both approaches compute each value once. One recurses with a cache; the other iterates from the bottom.
Approach 1: Memoised Recursion (Recommended)
Idea: recurse as in the definition, but look each n up in a HashMap before computing it.
- Return
nfor 0 and 1. - Return the cached value if there is one.
- Otherwise compute
fib(n - 1) + fib(n - 2), store it and return it.
Time: O(n) · Space: O(n).
Approach 2: Iterative Bottom-Up
Idea: keep the last two values and move forward n times.
- Start with
a = 0andb = 1. - Repeat
ntimes:next = a + b,a = b,b = next. - Return
a.
Time: O(n) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Memoised Recursion (Recommended)
The cache is shared between calls, so later requests are answered instantly.
import java.util.HashMap;
import java.util.Map;
public class FibMemo {
private static final Map<Integer, Long> memo = new HashMap<>();
// Plain recursion recomputes fib(n - 2) inside fib(n - 1); the cache stores each value once
static long fib(int n) {
if (n < 2) {
return n;
}
Long cached = memo.get(n);
if (cached != null) {
return cached;
}
long value = fib(n - 1) + fib(n - 2);
memo.put(n, value);
return value;
}
public static void main(String[] args) {
for (int n : new int[] {0, 1, 10, 50, 90}) {
System.out.println("fib(" + n + ") = " + fib(n));
}
}
}
fib(0) = 0 fib(1) = 1 fib(10) = 55 fib(50) = 12586269025 fib(90) = 2880067194370816120
Time complexity: O(n) · Space complexity: O(n).
Approach 2: Iterative Bottom-Up
No recursion and no cache, so it is the most memory-efficient version.
public class FibIterative {
// Bottom-up: keep only the last two values
static long fib(int n) {
long a = 0, b = 1;
for (int i = 0; i < n; i++) {
long next = a + b;
a = b;
b = next;
}
return a;
}
public static void main(String[] args) {
for (int n : new int[] {0, 1, 10, 50, 90}) {
System.out.println("fib(" + n + ") = " + fib(n));
}
}
}
fib(0) = 0 fib(1) = 1 fib(10) = 55 fib(50) = 12586269025 fib(90) = 2880067194370816120
Time complexity: O(n) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Memoised Recursion | O(n) | O(n) |
| Iterative Bottom-Up | O(n) | O(1) |
Here n is the index. Both approaches do n additions; the memoised version stores n values and uses n stack frames.
Interview Tips and Recommended Approach
- Show the exponential tree first, then explain how the cache removes repeated branches.
- Watch the range.
fib(93)overflows along; useBigIntegerbeyond that. - Know the faster method. Matrix exponentiation or fast doubling gives
O(log n). - Generalise. Memoization applies to any recursion with overlapping subproblems.
Recommended Approach: Memoised Recursion
Why: it keeps the natural recursive definition while removing all repeated work, which is the clearest introduction to memoization.