Challenge - 5 Problems
DP vs Recursion vs Greedy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Recursive Fibonacci with Memoization
What is the output of the following C code that calculates the 5th Fibonacci number using recursion with memoization?
DSA C
#include <stdio.h> int fib(int n, 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; int memo[6]; for (int i = 0; i <= n; i++) memo[i] = -1; int result = fib(n, memo); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts with 0, 1, 1, 2, 3, 5...
✗ Incorrect
The 5th Fibonacci number (0-based index) is 5. The code uses memoization to avoid repeated calculations.
🧠 Conceptual
intermediate1:30remaining
Choosing Greedy vs Dynamic Programming
Which problem characteristic best indicates that a greedy algorithm will NOT always produce the optimal solution, and dynamic programming should be considered instead?
Attempts:
2 left
💡 Hint
Think about when greedy fails and DP helps.
✗ Incorrect
When a problem has overlapping subproblems and optimal substructure, greedy may fail because local choices don't guarantee global optimum. DP handles this by exploring all subproblems.
🔧 Debug
advanced2:30remaining
Why Does This Greedy Coin Change Fail?
Consider the coin denominations {1, 3, 4} and the amount 6. The greedy algorithm picks the largest coin possible at each step. What is the output of the code below, and why does it fail to find the minimum number of coins?
DSA C
#include <stdio.h> int minCoins(int amount) { int coins[] = {4, 3, 1}; int count = 0; int i = 0; while (amount > 0) { if (coins[i] <= amount) { amount -= coins[i]; count++; } else { i++; } } return count; } int main() { int amount = 6; printf("%d\n", minCoins(amount)); return 0; }
Attempts:
2 left
💡 Hint
Try to find the minimum coins manually and compare with greedy output.
✗ Incorrect
Greedy picks 4 first, then two 1s, total 3 coins. But minimum coins are two 3s. Greedy fails because local largest coin choice is not optimal here.
❓ Predict Output
advanced2:30remaining
Dynamic Programming Table for 0/1 Knapsack
What is the value stored in dp[3][5] after running the 0/1 Knapsack dynamic programming code below? Items have weights {1, 3, 4} and values {15, 20, 30}, capacity is 5.
DSA C
#include <stdio.h> int max(int a, int b) { return (a > b) ? a : b; } int main() { int weights[] = {1, 3, 4}; int values[] = {15, 20, 30}; int n = 3, W = 5; int dp[4][6] = {0}; for (int i = 1; i <= n; i++) { for (int w = 1; w <= W; w++) { if (weights[i-1] <= w) { dp[i][w] = max(dp[i-1][w], dp[i-1][w - weights[i-1]] + values[i-1]); } else { dp[i][w] = dp[i-1][w]; } } } printf("%d\n", dp[3][5]); return 0; }
Attempts:
2 left
💡 Hint
Try to fill the dp table step by step for capacity 5.
✗ Incorrect
The maximum value for capacity 5 is 45, obtained by choosing items with weights 1 and 4 (values 15 + 30).
🧠 Conceptual
expert2:00remaining
When to Prefer Recursion with Memoization Over Iterative DP
In which scenario is recursion with memoization preferred over iterative dynamic programming?
Attempts:
2 left
💡 Hint
Think about when memoization avoids unnecessary computations.
✗ Incorrect
Memoization computes only needed states on demand, saving time and memory when many states are not required. Iterative DP computes all states regardless.