Challenge - 5 Problems
Fibonacci DP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fibonacci DP with memoization
What is the output of this Go code that calculates the 7th Fibonacci number 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(7, memo)) }
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13, 21...
✗ Incorrect
The 7th Fibonacci number (starting from 0) is 13 if counting from zero index, but here n=7 means the 7th number starting from 0 index, so fib(7) = 13. However, the code counts fib(0)=0, fib(1)=1, so fib(7) = fib(6)+fib(5) = 8+13=21.
❓ Predict Output
intermediateOutput of Fibonacci DP with tabulation
What is the output of this Go code that calculates the 10th Fibonacci number using bottom-up tabulation?
DSA Go
package main import "fmt" func fib(n int) int { if n <= 1 { return n } dp := make([]int, n+1) dp[0], dp[1] = 0, 1 for i := 2; i <= n; i++ { dp[i] = dp[i-1] + dp[i-2] } return dp[n] } func main() { fmt.Println(fib(10)) }
Attempts:
2 left
💡 Hint
The 10th Fibonacci number starting from 0 is 55.
✗ Incorrect
The code uses bottom-up DP to calculate fib(10). The sequence is 0,1,1,2,3,5,8,13,21,34,55. So fib(10) = 55.
🔧 Debug
advancedIdentify the error in Fibonacci DP code
What error will this Go code produce when calculating Fibonacci 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(5, memo)) }
Attempts:
2 left
💡 Hint
Check how memoization checks if a value is stored.
✗ Incorrect
The original code checked if memo[n] != 0 to decide if cached, but fib(0) = 0, so it never caches zero values properly. This causes incorrect results for fib(2) and others, returning 0 incorrectly. This fixed code uses the 'ok' idiom to check presence in map, so it returns correct results.
🧠 Conceptual
advancedTime complexity of Fibonacci DP
What is the time complexity of calculating the nth Fibonacci number using dynamic programming with memoization or tabulation?
Attempts:
2 left
💡 Hint
DP avoids repeated calculations by storing results.
✗ Incorrect
Dynamic programming calculates each Fibonacci number once, so it runs in linear time O(n).
🚀 Application
expertFind the 15th Fibonacci number using DP
Using dynamic programming, what is the 15th Fibonacci number (starting with fib(0) = 0)?
Attempts:
2 left
💡 Hint
The sequence starts 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610...
✗ Incorrect
The 15th Fibonacci number (index 15) is 610 in the sequence starting at 0.
