AlgoFrames

Which calls hit the cache

Standard · Trace the algorithm · about 6 min

The trace builds the full call tree of fib(5) without a memo. Now imagine the same code with a memo, filled left to right as the calls return.

Click every call in the tree that the memo version would answer straight from the memo. Calls inside a subtree the memo skips are never made, so they are not hits.

Example 1

Input:

{"n":5,"memo":0}

Output:

5

15 calls. With a memo only 9 of them happen; find the lookups among them.

Constraints

  • The trace uses fib(5) without a memo.
  • Base cases (n < 2) return at once, so they are not memo hits.

Learn the idea first: Overlapping subproblems and memoization, a visual lesson.

More Overlapping subproblems and memoization problems