Challenge - 5 Problems
Memoization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fibonacci with Memoization
What is the output of the following code that uses memoization to compute Fibonacci numbers?
DSA Python
def fib(n, 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(6))
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts 0,1,1,2,3,5,8,13...
✗ Incorrect
The Fibonacci sequence for n=6 is 8. The function uses memoization to avoid repeated calculations.
❓ Predict Output
intermediateOutput of Factorial with Memoization
What is the output of this factorial function using memoization?
DSA Python
def factorial(n, memo={}): if n in memo: return memo[n] if n == 0: return 1 memo[n] = n * factorial(n-1, memo) return memo[n] print(factorial(5))
Attempts:
2 left
💡 Hint
Factorial of 5 is 5*4*3*2*1.
✗ Incorrect
The factorial of 5 is 120. Memoization stores intermediate results to speed up recursion.
🔧 Debug
advancedIdentify the error in memoized Fibonacci
What error does this code raise when called with fib(4)?
DSA Python
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] print(fib(4))
Attempts:
2 left
💡 Hint
Check the order of base case and memo check.
✗ Incorrect
The code runs correctly and outputs 3 for fib(4). The base case is checked before memo lookup, so no error occurs.
❓ Predict Output
advancedOutput of Counting Paths with Memoization
What is the output of this code that counts paths in a grid using memoization?
DSA Python
def count_paths(m, n, memo={}): if m == 1 or n == 1: return 1 if (m, n) in memo: return memo[(m, n)] memo[(m, n)] = count_paths(m-1, n, memo) + count_paths(m, n-1, memo) return memo[(m, n)] print(count_paths(3, 3))
Attempts:
2 left
💡 Hint
Number of paths in a 3x3 grid is a known combinatorial value.
✗ Incorrect
The number of paths from top-left to bottom-right in a 3x3 grid moving only right or down is 4 choose 2 = 6, the function returns 6 paths for 2x2 steps. Here m=3,n=3 means 2 steps right and 2 steps down, total 4 steps, combinations 6. So output is 6.
❓ Predict Output
expertOutput of Memoized Edit Distance Calculation
What is the output of this memoized recursive function calculating edit distance between 'kitten' and 'sitting'?
DSA Python
def edit_distance(s1, s2, i=0, j=0, memo={}): if (i, j) in memo: return memo[(i, j)] if i == len(s1): return len(s2) - j if j == len(s2): return len(s1) - i if s1[i] == s2[j]: memo[(i, j)] = edit_distance(s1, s2, i+1, j+1, memo) else: insert = 1 + edit_distance(s1, s2, i, j+1, memo) delete = 1 + edit_distance(s1, s2, i+1, j, memo) replace = 1 + edit_distance(s1, s2, i+1, j+1, memo) memo[(i, j)] = min(insert, delete, replace) return memo[(i, j)] print(edit_distance('kitten', 'sitting'))
Attempts:
2 left
💡 Hint
Edit distance counts minimum edits to convert one string to another.
✗ Incorrect
The edit distance between 'kitten' and 'sitting' is 3 (replace k->s, replace e->i, insert g). The memoization avoids repeated calculations.
