What is memoization in the context of dynamic programming?
Memoization is a technique where we store the results of expensive function calls and reuse them when the same inputs occur again, avoiding repeated calculations.
Click to reveal answer
intermediate
How does top-down dynamic programming differ from bottom-up dynamic programming?
Top-down DP uses recursion with memoization to solve problems by breaking them into smaller subproblems and storing results. Bottom-up DP solves subproblems iteratively starting from the smallest, building up to the final solution.
Click to reveal answer
beginner
In memoization, what data structure is commonly used to store intermediate results?
A dictionary or array is commonly used to store intermediate results (memo) so that repeated calls with the same inputs can return the stored result immediately.
Click to reveal answer
intermediate
Why is memoization useful in recursive algorithms?
Memoization prevents the exponential explosion of recursive calls by saving results of subproblems, turning an exponential time algorithm into a polynomial time one.
Click to reveal answer
beginner
What is the base case in a top-down DP recursive function?
The base case is the simplest subproblem that can be solved directly without recursion, often returning a fixed value to stop further recursive calls.
Click to reveal answer
What does memoization primarily help to avoid in recursive functions?
ARepeated calculations of the same subproblems
BUsing too much memory
CWriting iterative loops
DSorting data
✗ Incorrect
Memoization stores results of subproblems to avoid recalculating them, improving efficiency.
Which data structure is commonly used to implement memoization in Python?
AList or dictionary
BQueue
CStack
DSet
✗ Incorrect
Lists or dictionaries are used to store computed results for quick lookup.
In top-down DP, what triggers the recursive calls?
ASorting the input
BBreaking the problem into smaller subproblems
CIterating over all elements
DUsing loops only
✗ Incorrect
Top-down DP solves the problem by recursively breaking it into smaller subproblems.
What is the main difference between memoization and tabulation?
Hint: Fibonacci(5) = 5 using memoized recursion [OK]
Common Mistakes:
Confusing Fibonacci sequence values
Expecting 8 instead of 5 for fib(5)
Thinking code causes error due to default dict
4. Identify the error in this memoized Fibonacci function:
def fib(n, memo={}):
if n <= 1:
return n
if n in memo:
return memo[n]
memo[n] = fib(n-1, memo) + fib(n-2, memo)
return memo[n]
medium
A. Missing base case for n=0
B. Function lacks return statement
C. Memo dictionary is not used correctly
D. Default mutable argument causes shared state across calls
Solution
Step 1: Analyze default argument usage
Using memo={} as default argument creates shared dictionary across calls, causing unexpected behavior.
Step 2: Identify correct practice
Default mutable arguments should be avoided; better to use None and initialize inside function.
Final Answer:
Default mutable argument causes shared state across calls -> Option D
Quick Check:
Mutable default argument = bug [OK]
Hint: Avoid mutable default args like {} in function definitions [OK]
Common Mistakes:
Ignoring mutable default argument issue
Thinking base case is missing
Assuming memo dictionary usage is wrong
5. You want to find the number of ways to climb a staircase of n steps, taking 1 or 2 steps at a time. Which memoized top-down function correctly solves this problem?
hard
A. def climb(n, memo=None):
if memo is None:
memo = {}
if n < 0:
return 0
if n == 0:
return 1
if n in memo:
return memo[n]
memo[n] = climb(n-1, memo) + climb(n-2, memo)
return memo[n]
B. def climb(n):
if n <= 1:
return n
return climb(n-1) + climb(n-2)
C. def climb(n, memo={}):
if n == 0:
return 0
if n in memo:
return memo[n]
memo[n] = climb(n-1, memo) + climb(n-2, memo)
return memo[n]
D. def climb(n, memo=None):
if memo is None:
memo = []
if n <= 1:
return 1
if n in memo:
return memo[n]
memo[n] = climb(n-1, memo) + climb(n-2, memo)
return memo[n]
Solution
Step 1: Understand problem and base cases
Number of ways to climb n steps with 1 or 2 steps is classic DP with base cases: ways(0)=1, ways(-1)=0.
Step 2: Check each option for correctness
def climb(n, memo=None):
if memo is None:
memo = {}
if n < 0:
return 0
if n == 0:
return 1
if n in memo:
return memo[n]
memo[n] = climb(n-1, memo) + climb(n-2, memo)
return memo[n] correctly initializes memo, handles negative and zero cases, and uses memoization properly. Others have wrong base cases or memo usage.
Final Answer:
Option A function correctly solves the problem -> Option A
Quick Check:
Proper base cases + memo dict = correct solution [OK]
Hint: Base cases and memo dict initialization are key [OK]
Common Mistakes:
Wrong base case returns
Using mutable default memo argument
Using list instead of dict for memo
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.