Memoization to Optimize Recursion in DSA Python - Time & Space Complexity
When we use recursion, some calculations repeat many times. Memoization helps by remembering results to avoid repeating work.
We want to know how this remembering changes the time it takes to run the code.
Analyze the time complexity of the following code snippet.
def fib_memo(n, memo=None):
if memo is None:
memo = {}
if n in memo:
return memo[n]
if n <= 1:
return n
memo[n] = fib_memo(n-1, memo) + fib_memo(n-2, memo)
return memo[n]
This code calculates the nth Fibonacci number using recursion with memoization to store results.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls to fib_memo for smaller numbers.
- How many times: Each unique number from 0 to n is computed once and stored.
Each number up to n is calculated once, so the work grows steadily as n grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calculations |
| 100 | About 100 calculations |
| 1000 | About 1000 calculations |
Pattern observation: The number of operations grows roughly in a straight line with n.
Time Complexity: O(n)
This means the time to finish grows directly with the input size because each result is calculated once and reused.
[X] Wrong: "Memoization does not change the time complexity of recursion."
[OK] Correct: Without memoization, many calls repeat work, causing exponential time. Memoization stores results, reducing repeated work and lowering time complexity to linear.
Understanding how memoization improves recursion shows you can make slow solutions fast by smartly remembering work done. This skill is valuable for solving many problems efficiently.
"What if we removed memoization and used plain recursion? How would the time complexity change?"
Practice
memoization in recursive functions?Solution
Step 1: Understand what memoization does
Memoization saves the results of function calls so that if the same inputs occur again, the function can return the saved result instead of recalculating.Step 2: Identify the benefit of memoization
This saves time by avoiding repeated calculations, making recursive functions faster.Final Answer:
To store results of expensive function calls and reuse them -> Option CQuick Check:
Memoization = Store and reuse results [OK]
- Thinking memoization slows down the function
- Confusing memoization with converting recursion to loops
- Believing memoization always increases memory without benefit
Solution
Step 1: Understand default mutable arguments problem
Using a mutable default argument like a dictionary directly can cause unexpected behavior because the same dictionary is shared across calls.Step 2: Use None as default and initialize inside
Setting memo=None and then initializing memo = {} inside the function avoids this problem and is the correct pattern.Final Answer:
def fib(n, memo=None): memo = {} -> Option BQuick Check:
Use None default and initialize dict inside [OK]
- Using mutable default arguments directly
- Using list instead of dictionary for memo
- Setting memo default to 0 which is invalid
def fib(n, memo=None):
if memo is None:
memo = {}
if n in memo:
return memo[n]
if n <= 1:
return n
memo[n] = fib(n-1, memo) + fib(n-2, memo)
return memo[n]
print(fib(5))Solution
Step 1: Trace fib(5) with memoization
fib(5) = fib(4) + fib(3), fib(4) = fib(3) + fib(2), and so on. Using memoization avoids repeated calls.Step 2: Calculate fib(5)
fib(0)=0, fib(1)=1, fib(2)=1, fib(3)=2, fib(4)=3, fib(5)=5.Final Answer:
5 -> Option DQuick Check:
fib(5) = 5 [OK]
- Confusing fib(5) with fib(6)
- Ignoring memo and recalculating values
- Returning None due to missing return
def factorial(n, memo=None):
if memo is None:
memo = {}
if n == 0:
return 1
if n in memo:
return memo[n]
memo[n] = n * factorial(n-1)
return memo[n]Solution
Step 1: Check recursive call parameters
The recursive call factorial(n-1) misses passing memo, so memo is reset each time, losing stored results.Step 2: Fix by passing memo in recursive call
Change to factorial(n-1, memo) to keep using the same memo dictionary.Final Answer:
Add memo argument in recursive call: factorial(n-1, memo) -> Option AQuick Check:
Pass memo in recursion to keep cache [OK]
- Forgetting to pass memo in recursive call
- Changing base case unnecessarily
- Using list instead of dictionary for memo
n stairs, taking 1 or 2 steps at a time. Which memoization approach below correctly avoids redundant calculations and handles edge cases?Solution
Step 1: Understand memoization best practice
Usingmemo=Nonedefault and initializing inside avoids shared mutable defaults and ensures fresh memo per call.Step 2: Pass memo in recursive calls and handle base cases
Passing memo keeps cache consistent. Base cases n=0 (1 way) and n=1 (1 way) prevent infinite recursion and define correct results.Final Answer:
Use memo=None default, initialize inside, pass memo in recursion, handle base cases -> Option AQuick Check:
Proper memo init and passing + base cases = correct [OK]
- Using global variables causing side effects
- Using mutable default arguments directly
- Ignoring base cases causing infinite recursion
Ready to prove it?
Pass the test and this pattern moves unknown
