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 Go code that uses memoization to compute Fibonacci numbers?
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 Fibonacci sequence is 0,1,1,2,3,5,8,13,... so fib(6) = 8.
🧠 Conceptual
intermediateMemoization Purpose in Top Down DP
What is the main purpose of using memoization in top-down dynamic programming?
Attempts:
2 left
💡 Hint
Think about how recursion can repeat work.
✗ Incorrect
Memoization saves results of expensive function calls and returns the cached result when the same inputs occur again, avoiding repeated work.
🔧 Debug
advancedIdentify the Error in Memoized Fibonacci
What error will this Go code produce when run?
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() { var memo map[int]int fmt.Println(fib(5, memo)) }
Attempts:
2 left
💡 Hint
Check how the map memo is initialized.
✗ Incorrect
The map memo is declared but not initialized, so it is nil. Writing to a nil map causes a runtime panic.
🚀 Application
advancedMemoization in Climbing Stairs Problem
Given the climbing stairs problem where you can climb 1 or 2 steps, what is the output of this memoized Go function for input 4?
DSA Go
package main import "fmt" func climbStairs(n int, memo map[int]int) int { if n <= 2 { return n } 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.
❓ Predict Output
expertOutput of Memoized Coin Change Ways
What is the output of this Go code that uses memoization to count ways to make change for amount 5 with coins [1,2,5]?
DSA Go
package main import "fmt" func coinChange(coins []int, amount int, index int, memo map[string]int) int { key := fmt.Sprintf("%d-%d", amount, index) if val, ok := memo[key]; ok { return val } if amount == 0 { return 1 } if amount < 0 || index == len(coins) { return 0 } include := coinChange(coins, amount-coins[index], index, memo) exclude := coinChange(coins, amount, index+1, memo) memo[key] = include + exclude return memo[key] } func main() { coins := []int{1, 2, 5} memo := make(map[string]int) fmt.Println(coinChange(coins, 5, 0, memo)) }
Attempts:
2 left
💡 Hint
Count all combinations of coins that sum to 5.
✗ Incorrect
The ways to make 5 are: 1+1+1+1+1, 1+1+1+2, 1+2+2, 5. Total 4 ways.
