Compute fib(n) recursively. Cache each fib(k) in a memo map so each subproblem is solved at most once.

Algorithm

Canonical input n = 6 produces fib(6) = 8. Replay highlights every memo write and every cache hit.

memoization A `HashMap<Integer, Integer>` cache stores each completed subproblem. Before recursing, check the memo: a hit returns immediately, a miss descends.
explicit memo parameter Pass the memo as an explicit parameter so the lesson stays about caching, not language-level scoping.

Basic Implementation

Basic.java
Replay: real traced execution (multi-file project)
import java.util.HashMap;
import java.util.Map;

public class Basic {
    public static void main(String[] args) {
        Map<Integer, Integer> memo = new HashMap<>();
        int result = fib(6, memo);
        System.out.println(result);
        System.out.println(memo);
    }

    private static int fib(int n, Map<Integer, Integer> memo) {
        if (memo.containsKey(n)) {
            return memo.get(n);
        }
        if (n < 2) {
            memo.put(n, n);
            return n;
        }
        int value = fib(n - 1, memo) + fib(n - 2, memo);
        memo.put(n, value);
        return value;
    }
}
  1. memo ← {}, action ← miss -> descend fib(5)

    19}20int value = fib(n - 1, memo) + fib(n - 2, memo);21memo.put(n, value);
    values this step{}memomiss -> descend fib(5)action6n
  2. memo ← {}, action ← miss -> descend fib(4)

    19}20int value = fib(n - 1, memo) + fib(n - 2, memo);21memo.put(n, value);
    values this step{}memomiss -> descend fib(4)action5n
  3. memo ← {}, action ← miss -> descend fib(3)

    19}20int value = fib(n - 1, memo) + fib(n - 2, memo);21memo.put(n, value);
    values this step{}memomiss -> descend fib(3)action4n
  4. memo ← {}, action ← miss -> descend fib(2)

    19}20int value = fib(n - 1, memo) + fib(n - 2, memo);21memo.put(n, value);
    values this step{}memomiss -> descend fib(2)action3n
  5. memo ← {}, action ← miss -> descend fib(1)

    19}20int value = fib(n - 1, memo) + fib(n - 2, memo);21memo.put(n, value);
    values this step{}memomiss -> descend fib(1)action2n
  6. memo ← {1=1}, action ← base 1; memo.put(1, 1); return

    19}20int value = fib(n - 1, memo) + fib(n - 2, memo);21memo.put(n, value);
    values this step{1=1}memobase 1; memo.put(1, 1); returnaction1n
  7. memo ← {0=0, 1=1}, action ← base 0; memo.put(0, 0); fib(2)=1; memo.put(2, 1)

    19}20int value = fib(n - 1, memo) + fib(n - 2, memo);21memo.put(n, value);
    values this step{0=0, 1=1}memobase 0; memo.put(0, 0); fib(2)=1; memo.put(2, 1)action0n
  8. memo ← {0=0, 1=1, 2=1, 3=2}, action ← hit 1; fib(3)=2; memo.put(3, 2)

    19}20int value = fib(n - 1, memo) + fib(n - 2, memo);21memo.put(n, value);
    values this step{0=0, 1=1, 2=1, 3=2}memohit 1; fib(3)=2; memo.put(3, 2)action1n
  9. memo ← {0=0, 1=1, 2=1, 3=2, 4=3}, action ← hit 1; fib(4)=3; memo.put(4, 3)

    19}20int value = fib(n - 1, memo) + fib(n - 2, memo);21memo.put(n, value);
    values this step{0=0, 1=1, 2=1, 3=2, 4=3}memohit 1; fib(4)=3; memo.put(4, 3)action2n
  10. memo ← {0=0, 1=1, 2=1, 3=2, 4=3, 5=5}, action ← hit 2; fib(5)=5; memo.put(5, 5)

    19}20int value = fib(n - 1, memo) + fib(n - 2, memo);21memo.put(n, value);
    values this step{0=0, 1=1, 2=1, 3=2, 4=3, 5=5}memohit 2; fib(5)=5; memo.put(5, 5)action3n
  11. memo ← {0=0, 1=1, 2=1, 3=2, 4=3, 5=5, 6=8}, action ← hit 3; fib(6)=8; memo.put(6, 8)

    19}20int value = fib(n - 1, memo) + fib(n - 2, memo);21memo.put(n, value);
    values this step{0=0, 1=1, 2=1, 3=2, 4=3, 5=5, 6=8}memohit 3; fib(6)=8; memo.put(6, 8)action4n
  12. stdout ← 8

    7int result = fib(6, memo);8System.out.println(result);9System.out.println(memo);
    values this step8stdout8result

Complexity

  • Time: O(n) with memoization (vs. O(2^n) without)
  • Space: O(n) memo + O(n) call stack

Implementation notes

  • Java: Map<Integer, Integer> memo = new HashMap<>(); passed explicitly to fib(int n, Map<Integer, Integer> memo).
  • The replay shows the call stack on one side and the memo map on the other so memo writes and cache hits are visually distinct.