0
0
DSA Cprogramming~20 mins

DP vs Recursion vs Greedy Choosing the Right Tool in DSA C - Compare & Choose

Choose your learning style9 modes available
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
intermediate
2: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;
}
A3
B8
C5
D13
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts with 0, 1, 1, 2, 3, 5...
🧠 Conceptual
intermediate
1: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?
AThe problem has overlapping subproblems and optimal substructure
BThe problem can be solved by making locally optimal choices that lead to a global optimum
CThe problem has no overlapping subproblems and no optimal substructure
DThe problem requires exploring all possible combinations to find the best solution
Attempts:
2 left
💡 Hint
Think about when greedy fails and DP helps.
🔧 Debug
advanced
2: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;
}
A2 (coins used: 3 + 3), greedy works correctly here
B3 (coins used: 4 + 1 + 1), greedy fails because it doesn't find the minimum 2 coins (3 + 3)
C4 (coins used: 1 + 1 + 1 + 3), greedy fails due to wrong coin order
D1 (coin used: 6), greedy fails because coin 6 doesn't exist
Attempts:
2 left
💡 Hint
Try to find the minimum coins manually and compare with greedy output.
Predict Output
advanced
2: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;
}
A45
B35
C65
D50
Attempts:
2 left
💡 Hint
Try to fill the dp table step by step for capacity 5.
🧠 Conceptual
expert
2:00remaining
When to Prefer Recursion with Memoization Over Iterative DP
In which scenario is recursion with memoization preferred over iterative dynamic programming?
AWhen the problem size is very large and iterative solutions are faster
BWhen the problem requires filling a complete DP table for all subproblems
CWhen the problem has no overlapping subproblems
DWhen the problem has a complex state space and not all states need to be computed
Attempts:
2 left
💡 Hint
Think about when memoization avoids unnecessary computations.