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 the following C++ code that calculates the 6th Fibonacci number using memoization?
DSA C++
#include <iostream> #include <vector> using namespace std; int fib(int n, vector<int>& memo) { if (n <= 1) return n; if (memo[n] != -1) return memo[n]; memo[n] = fib(n-1, memo) + fib(n-2, memo); return memo[n]; } int main() { int n = 6; vector<int> memo(n+1, -1); cout << fib(n, memo) << endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8...
✗ Incorrect
The 6th Fibonacci number (0-based index) is 8. The code uses memoization to avoid repeated calculations.
❓ Predict Output
intermediateOutput of Bottom-Up Fibonacci DP
What is the output of this C++ code that calculates the 7th Fibonacci number using bottom-up dynamic programming?
DSA C++
#include <iostream> #include <vector> using namespace std; int fib(int n) { vector<int> dp(n+1, 0); dp[1] = 1; for (int i = 2; i <= n; i++) { dp[i] = dp[i-1] + dp[i-2]; } return dp[n]; } int main() { cout << fib(7) << endl; return 0; }
Attempts:
2 left
💡 Hint
Check the Fibonacci sequence values carefully for index 7.
✗ Incorrect
The 7th Fibonacci number is 13 using 0-based indexing starting with 0,1,...
🔧 Debug
advancedIdentify the runtime error in Fibonacci DP code
What runtime error will this C++ code produce when calculating fib(5) using memoization?
DSA C++
#include <iostream> #include <vector> using namespace std; int fib(int n, vector<int>& memo) { if (n <= 1) return n; if (memo[n] != 0) return memo[n]; memo[n] = fib(n-1, memo) + fib(n-2, memo); return memo[n]; } int main() { int n = 5; vector<int> memo(n+1); cout << fib(n, memo) << endl; return 0; }
Attempts:
2 left
💡 Hint
Observe that the base case if (n <= 1) return n; comes before the memo check. memo[0] and memo[1] are never read.
✗ Incorrect
No runtime error. The code correctly outputs 5, the 5th Fibonacci number. The base cases are handled before checking the memo, and since Fibonacci numbers for n >= 2 are positive, the != 0 check works as a sentinel for uncomputed values.
🧠 Conceptual
advancedTime complexity of Fibonacci DP approaches
What is the time complexity of calculating the nth Fibonacci number using dynamic programming (either memoization or bottom-up)?
Attempts:
2 left
💡 Hint
Dynamic programming avoids repeated calculations by storing results.
✗ Incorrect
Both memoization and bottom-up DP calculate each Fibonacci number once, resulting in linear time O(n).
🚀 Application
expertFind the number of ways to climb stairs using Fibonacci DP
You can climb 1 or 2 steps at a time. How many distinct ways are there to climb 5 steps? The code below uses Fibonacci DP logic to solve this. What is the output?
DSA C++
#include <iostream> #include <vector> using namespace std; int climbStairs(int n) { vector<int> dp(n+1, 0); dp[0] = 1; dp[1] = 1; for (int i = 2; i <= n; i++) { dp[i] = dp[i-1] + dp[i-2]; } return dp[n]; } int main() { cout << climbStairs(5) << endl; return 0; }
Attempts:
2 left
💡 Hint
This problem maps to Fibonacci numbers shifted by one index.
✗ Incorrect
Number of ways to climb 5 steps is the 6th Fibonacci number (starting dp[0]=1), which is 8.
