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 with Memoization
What is the output of this code that calculates the 6th Fibonacci number using memoization (top-down dynamic programming)?
DSA Python
def fib_memo(n, memo={}): if n in memo: return memo[n] if n <= 1: return n memo[n] = fib_memo(n-1, memo) + fib_memo(n-2, memo) return memo[n] print(fib_memo(6))
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8...
✗ Incorrect
The 6th Fibonacci number (0-based index) is 8. The function uses memoization to avoid repeated calculations.
❓ Predict Output
intermediateOutput of Fibonacci with Tabulation
What is the output of this code that calculates the 7th Fibonacci number using tabulation (bottom-up dynamic programming)?
DSA Python
def fib_tab(n): if n <= 1: return n dp = [0] * (n+1) dp[1] = 1 for i in range(2, n+1): dp[i] = dp[i-1] + dp[i-2] return dp[n] print(fib_tab(7))
Attempts:
2 left
💡 Hint
Count carefully starting from 0: 0,1,1,2,3,5,8,13...
✗ Incorrect
The 7th Fibonacci number (0-based) is 13. The code builds the sequence from the bottom up.
🔧 Debug
advancedIdentify the error in Fibonacci memoization code
What error does this code raise when trying to compute fib_memo(5)?
DSA Python
def fib_memo(n, memo={}): if n <= 1: return n if n in memo: return memo[n] memo[n] = fib_memo(n-1, memo) + fib_memo(n-2, memo) return memo[n] print(fib_memo(5))
Attempts:
2 left
💡 Hint
Check the order of base case and memo check.
✗ Incorrect
The code runs correctly and outputs 5. The base case is checked before memo lookup, so no infinite recursion occurs.
❓ Predict Output
advancedOutput of Fibonacci with space optimization
What is the output of this code that calculates the 8th Fibonacci number using space optimized bottom-up approach?
DSA Python
def fib_optimized(n): if n <= 1: return n a, b = 0, 1 for _ in range(2, n+1): a, b = b, a + b return b print(fib_optimized(8))
Attempts:
2 left
💡 Hint
Remember the sequence: 0,1,1,2,3,5,8,13,21,34...
✗ Incorrect
The 8th Fibonacci number (0-based) is 21. The code uses two variables to save space.
🧠 Conceptual
expertWhy use dynamic programming for Fibonacci?
Which of the following best explains why dynamic programming is used to calculate Fibonacci numbers efficiently?
Attempts:
2 left
💡 Hint
Think about how repeated work is avoided.
✗ Incorrect
Dynamic programming stores results of smaller problems to avoid recalculating them, making Fibonacci calculation efficient.
