Back to Practice
#0227

Memoized Fibonacci

MediumPython25 min20 XP

Problem

Return the nth Fibonacci number where fib(0)=0 and fib(1)=1. Use memoization.

Why This Matters

Memoization is the simplest bridge from recursion to dynamic programming.

Function Signature

def fib(n):

Examples

Example 1
Inputn = 8
Output21

The sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Prefer readable Python before micro-optimizing.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputn = 8
Expected21

The sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21.

Hidden Test Categories
n = 0n = 1larger n such as 30