0
0
DSA Cprogramming~20 mins

Why Dynamic Programming and When Recursion Alone Fails in DSA C - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Recursive Fibonacci Without Memoization
What is the output of the following C code that calculates Fibonacci numbers using simple recursion without any optimization?
DSA C
#include <stdio.h>

int fib(int n) {
    if (n <= 1) return n;
    return fib(n-1) + fib(n-2);
}

int main() {
    printf("%d\n", fib(5));
    return 0;
}
A5
B8
C13
D3
Attempts:
2 left
💡 Hint
Recall the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, ...
Predict Output
intermediate
2:00remaining
Output of Fibonacci with Memoization
What is the output of this C code that calculates Fibonacci numbers using recursion with memoization?
DSA C
#include <stdio.h>

int memo[100] = {0};

int fib(int n) {
    if (n <= 1) return n;
    if (memo[n] != 0) return memo[n];
    memo[n] = fib(n-1) + fib(n-2);
    return memo[n];
}

int main() {
    printf("%d\n", fib(10));
    return 0;
}
A55
B89
C34
D144
Attempts:
2 left
💡 Hint
Remember fib(10) in Fibonacci sequence starting from fib(0)=0.
🧠 Conceptual
advanced
1:30remaining
Why Recursion Alone Can Be Inefficient
Why does simple recursion without memoization often fail or become inefficient for problems like Fibonacci sequence calculation?
ABecause recursion uses loops internally which slow down the program
BBecause recursion always uses more memory than iteration
CBecause recursion repeats the same calculations multiple times leading to exponential time complexity
DBecause recursion cannot handle base cases properly
Attempts:
2 left
💡 Hint
Think about how many times fib(2) is calculated in fib(5) without memoization.
Predict Output
advanced
2:00remaining
Output of Bottom-Up Dynamic Programming Fibonacci
What is the output of this C code that calculates Fibonacci numbers using bottom-up dynamic programming?
DSA C
#include <stdio.h>

int fib(int n) {
    int dp[100] = {0};
    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() {
    printf("%d\n", fib(7));
    return 0;
}
A21
B34
C55
D13
Attempts:
2 left
💡 Hint
Recall the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, ...
🧠 Conceptual
expert
1:30remaining
When to Prefer Dynamic Programming Over Recursion
In which scenario is dynamic programming preferred over simple recursion?
AWhen the problem can be solved only by iteration
BWhen the problem has no repeated calculations
CWhen recursion depth is always less than 10
DWhen the problem has overlapping subproblems and optimal substructure
Attempts:
2 left
💡 Hint
Think about problems where subproblems repeat and results can be reused.