00:00

Problem Statement

Input/Output Examples

Visual Explanation

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.

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.

Complexity Analysis

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 a long; use BigInteger beyond 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.