Skip to the workspace
Pebellslearn by building
Greedy, Backtracking & Dynamic Programming  ›  Lesson

Memoization & the Slot Filler

Dynamic Programming 35 minRemember each subproblem's answer so it's only ever solved once

dsa / Memoization & the Slot Filler

Explore each scene at your own pace.

01 · Counting fib the slow way

Counting fib the slow way

Memoization & the Slot Filler

Definition
Memoization speeds up a recursive solution by storing the answer to each subproblem the first time it is computed and reusing it whenever the same subproblem appears again.
In plain words
It is keeping a notebook of answers you have already worked out. Before doing a sum, you look in the notebook; if the answer is there you copy it, and if not you work it out once and write it down.

In short: some recursions ask the same question over and over — overlapping subproblems — and when the answer to a bigger question is built from answers to smaller ones — optimal substructure — that's a dynamic programming problem. Memoization keeps a notebook (a dict, or Python's @functools.cache) of every question already answered: check it before computing, write the answer after. The recursion is unchanged; only the repeated work disappears, usually turning exponential time into linear or polynomial time.

A notebook of answers: the first "fib(3)?" computes its way down and writes the answer into the notebook, the second "fib(3)?" is answered straight from it and the recursive calls underneath it never happen

Counting fib the slow way

Tunebox Radio fills gaps with station idents, and the scheduling screen shows how many different ident sequences could fill each gap. The obvious recursive count answers instantly for a 10-minute gap and freezes the app at 40. Fibonacci is the smallest program with the same disease:

python

21,891 calls to work out one number. Here is why: fib(3) is solved twice and fib(2) three times inside fib(5) alone, and every level roughly doubles the repeats:

watch it move · tree

A memo answers each repeated question from its notes, so whole subtrees of calls disappear.

Plain recursion makes all 15 of these calls, yet there are only 6 different questions. Binary tree, 15 nodes, height 5. At node fib(5).

1 / 8
What does this print?
calls = 0
def fib(n):
    global calls
    calls += 1
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)

fib(5)
print(calls)

The cost if you don't fix it: the running time grows like φⁿ ≈ 1.618ⁿ. fib(40) makes about 331 million calls.

Scene 1 of 7
Project context and further resources
You're building a piece ofTunebox — festival planner
This piece — count_fills(): Counts the ways to fill a time slot — without recomputing.
Scenario The Tunebox Radio scheduler shows how many ident sequences could fill each gap between shows. Long gaps have trillions, so the count has to come from remembered subproblems, not from listing them.
count_fills.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
count_fills(slot, lengths) → intHow many ident sequences fill the slot exactly.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.