Challenge - 5 Problems
Bottom-Up DP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Bottom-Up DP for Fibonacci
What is the output of the following C++ code that uses bottom-up dynamic programming to compute Fibonacci numbers?
DSA C++
int fib(int n) { int dp[n+1]; dp[0] = 0; dp[1] = 1; for(int i = 2; i <= n; i++) { dp[i] = dp[i-1] + dp[i-2]; } return dp[n]; } int main() { int result = fib(6); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Trace the Fibonacci sequence starting from 0,1 up to the 6th number.
✗ Incorrect
The Fibonacci sequence starts as 0,1,1,2,3,5,8,... The 6th Fibonacci number (0-based) is 8.
❓ Predict Output
intermediateOutput of Bottom-Up DP for Coin Change Ways
What is the output of this C++ code that calculates the number of ways to make change for amount 5 using coins {1,2} with bottom-up DP?
DSA C++
int countWays(int coins[], int n, int amount) { int dp[amount+1] = {0}; dp[0] = 1; for(int i = 0; i < n; i++) { for(int j = coins[i]; j <= amount; j++) { dp[j] += dp[j - coins[i]]; } } return dp[amount]; } int main() { int coins[] = {1, 2}; int result = countWays(coins, 2, 5); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Count all combinations of 1s and 2s that sum to 5.
✗ Incorrect
Ways to make 5: (1+1+1+1+1), (1+1+1+2), (1+2+2). Order doesn't matter, so total 3 combinations.
🧠 Conceptual
advancedUnderstanding DP Table Initialization
In bottom-up dynamic programming, why is it important to initialize the base cases in the DP table before filling the rest?
Attempts:
2 left
💡 Hint
Think about how smaller problems help solve bigger problems.
✗ Incorrect
Base cases give known answers for smallest subproblems, allowing the DP to build up solutions step-by-step.
🔧 Debug
advancedIdentify the Bug in Bottom-Up DP Code
What error will this C++ bottom-up DP code produce when run?
DSA C++
int fib(int n) { int dp[n]; dp[0] = 0; dp[1] = 1; for(int i = 2; i <= n; i++) { dp[i] = dp[i-1] + dp[i-2]; } return dp[n]; } int main() { std::cout << fib(5) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check array size and loop limits carefully.
✗ Incorrect
Array dp has size n but code accesses dp[n], which is out of bounds causing runtime error.
🚀 Application
expertMinimum Steps to One Using Bottom-Up DP
Given the code below, what is the value of dp[10] after execution? dp[i] stores the minimum steps to reduce i to 1 by either subtracting 1, dividing by 2 if divisible, or dividing by 3 if divisible.
DSA C++
int minSteps(int n) { int dp[n+1]; dp[1] = 0; for(int i = 2; i <= n; i++) { dp[i] = dp[i-1] + 1; if(i % 2 == 0) dp[i] = std::min(dp[i], dp[i/2] + 1); if(i % 3 == 0) dp[i] = std::min(dp[i], dp[i/3] + 1); } return dp[n]; } int main() { std::cout << minSteps(10) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Trace dp values from 1 to 10 carefully considering all operations.
✗ Incorrect
Minimum steps for 10: 10->9 (subtract 1), 9->3 (divide by 3), 3->1 (divide by 3), total 3 steps.
