Challenge - 5 Problems
Master of Overlapping Subproblems and Optimal Substructure
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 C++ code that calculates Fibonacci numbers using memoization?
DSA C++
#include <iostream> #include <vector> int fib(int n, std::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 = 5; std::vector<int> memo(n + 1, -1); std::cout << fib(n, memo) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5...
✗ Incorrect
The function calculates the 5th Fibonacci number (0-based index), which is 5.
🧠 Conceptual
intermediateIdentifying Overlapping Subproblems
Which of the following problems best demonstrates the concept of overlapping subproblems?
Attempts:
2 left
💡 Hint
Overlapping subproblems means the same smaller problems are solved multiple times.
✗ Incorrect
Fibonacci recursion without memoization recalculates the same Fibonacci numbers multiple times, showing overlapping subproblems.
🔧 Debug
advancedDebugging Memoization Implementation
What error does the following code produce when trying to compute the Fibonacci number with memoization?
DSA C++
#include <iostream> #include <vector> int fib(int n, std::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 = 4; std::vector<int> memo(n + 1, -1); std::cout << fib(n, memo) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check how the memo vector is passed to the function.
✗ Incorrect
The memo vector is passed by value, so updates inside fib do not persist. The function should take memo by reference to avoid this issue.
❓ Predict Output
advancedOutput of Dynamic Programming Table for Coin Change
What is the output of the following C++ code that calculates the number of ways to make change for amount 5 using coins {1, 2, 5}?
DSA C++
#include <iostream> #include <vector> int countWays(int amount, const std::vector<int>& coins) { std::vector<int> dp(amount + 1, 0); dp[0] = 1; for (int coin : coins) { for (int x = coin; x <= amount; ++x) { dp[x] += dp[x - coin]; } } return dp[amount]; } int main() { std::vector<int> coins = {1, 2, 5}; int amount = 5; std::cout << countWays(amount, coins) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Think about all combinations of coins that sum to 5.
✗ Incorrect
The ways are: {1,1,1,1,1}, {1,1,1,2}, {1,2,2}, {5}, total 4 ways.
🧠 Conceptual
expertOptimal Substructure in Shortest Path Problem
Which statement best describes the optimal substructure property in the shortest path problem?
Attempts:
2 left
💡 Hint
Optimal substructure means optimal solutions contain optimal subsolutions.
✗ Incorrect
In shortest path problems, any subpath of a shortest path is itself a shortest path, showing optimal substructure.
