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 this Go program that calculates Fibonacci numbers using memoization?
DSA Go
package main import "fmt" func fib(n int, memo map[int]int) int { if n <= 1 { return n } if val, ok := memo[n]; ok { return val } memo[n] = fib(n-1, memo) + fib(n-2, memo) return memo[n] } func main() { memo := make(map[int]int) fmt.Println(fib(6, memo)) }
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts 0,1,1,2,3,5,8,13...
✗ Incorrect
The code computes fib(6) = fib(5) + fib(4) = 5 + 3 = 8, with base cases fib(0)=0 and fib(1)=1. The output is 8.
❓ Predict Output
intermediateMemoization Map State After Calls
After running fib(5, memo) with memo initially empty, what is the state of the memo map?
DSA Go
package main import "fmt" func fib(n int, memo map[int]int) int { if n <= 1 { return n } if val, ok := memo[n]; ok { return val } memo[n] = fib(n-1, memo) + fib(n-2, memo) return memo[n] } func main() { memo := make(map[int]int) fib(5, memo) fmt.Println(memo) }
Attempts:
2 left
💡 Hint
Memo stores only computed values for n > 1.
✗ Incorrect
The base cases n=0 and n=1 return values directly without storing in memo. Only fib(2) to fib(5) are stored in memo. So memo contains keys 2,3,4,5 with their Fibonacci values.
🔧 Debug
advancedIdentify the Error in Memoized Factorial
What error will this Go code produce when calculating factorial with memoization?
DSA Go
package main import "fmt" func factorial(n int, memo map[int]int) int { if n == 0 { return 1 } if val, ok := memo[n]; ok { return val } memo[n] = n * factorial(n-1, memo) return memo[n] } func main() { memo := make(map[int]int) fmt.Println(factorial(-1, memo)) }
Attempts:
2 left
💡 Hint
Check base case and input value.
✗ Incorrect
The base case only handles n==0. For n=-1, the function calls factorial(-2), factorial(-3), ... infinitely causing stack overflow.
🧠 Conceptual
advancedWhy Use Memoization in Recursive Algorithms?
Which option best explains why memoization improves recursive algorithm performance?
Attempts:
2 left
💡 Hint
Think about repeated work in recursion.
✗ Incorrect
Memoization saves results of expensive function calls and returns cached results when the same inputs occur again, reducing time complexity.
❓ Predict Output
expertOutput of Memoized Climbing Stairs Problem
What is the output of this Go program that uses memoization to count ways to climb stairs?
DSA Go
package main import "fmt" func climbStairs(n int, memo map[int]int) int { if n <= 1 { return 1 } if val, ok := memo[n]; ok { return val } memo[n] = climbStairs(n-1, memo) + climbStairs(n-2, memo) return memo[n] } func main() { memo := make(map[int]int) fmt.Println(climbStairs(4, memo)) }
Attempts:
2 left
💡 Hint
Number of ways to climb 4 steps with 1 or 2 steps at a time.
✗ Incorrect
Ways to climb 4 steps: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2 = 5 ways total.
