0
0
DSA Cprogramming~20 mins

Fibonacci Using DP in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fibonacci DP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Fibonacci DP Array
What is the printed state of the DP array after computing fibonacci(6) using bottom-up dynamic programming?
DSA C
int fibonacci(int n) {
    int dp[7] = {0};
    dp[0] = 0;
    dp[1] = 1;
    for (int i = 2; i <= n; i++) {
        dp[i] = dp[i-1] + dp[i-2];
    }
    for (int i = 0; i <= n; i++) {
        printf("%d ", dp[i]);
    }
    return dp[n];
}

int main() {
    fibonacci(6);
    return 0;
}
A0 1 2 3 5 8 13
B0 1 1 3 4 7 11
C1 1 2 3 5 8 13
D0 1 1 2 3 5 8
Attempts:
2 left
💡 Hint
Remember that fibonacci starts with 0 and 1, then each next number is sum of previous two.
Predict Output
intermediate
2:00remaining
Value of fibonacci(7) with memoization
What is the value returned by fibonacci(7) using top-down memoization with dp array initialized to -1?
DSA C
int dp[8];

int fibonacci(int n) {
    if (dp[n] != -1) return dp[n];
    if (n <= 1) return n;
    dp[n] = fibonacci(n-1) + fibonacci(n-2);
    return dp[n];
}

int main() {
    for (int i = 0; i < 8; i++) dp[i] = -1;
    int result = fibonacci(7);
    printf("%d", result);
    return 0;
}
A8
B13
C21
D34
Attempts:
2 left
💡 Hint
Fibonacci sequence starts 0,1,1,2,3,5,8,13...
🔧 Debug
advanced
2:00remaining
Identify the error in DP Fibonacci code
What error will this code produce when computing fibonacci(5)?
DSA C
int fibonacci(int n) {
    int dp[5] = {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() {
    int result = fibonacci(5);
    printf("%d", result);
    return 0;
}
AReturns 5 correctly
BCompilation error: dp array size mismatch
CRuntime error: accessing dp[5] out of bounds
DReturns 0 always
Attempts:
2 left
💡 Hint
Check the size of dp array and the loop limits carefully.
🧠 Conceptual
advanced
2:00remaining
Time complexity of Fibonacci DP
What is the time complexity of computing fibonacci(n) using bottom-up dynamic programming with a dp array?
AO(n)
BO(2^n)
CO(n^2)
DO(log n)
Attempts:
2 left
💡 Hint
Each fibonacci number is computed once in bottom-up DP.
🚀 Application
expert
2:00remaining
Number of calls in naive vs memoized Fibonacci
How many times is fibonacci(2) computed in naive recursive fibonacci(5) vs memoized fibonacci(5)?
ANaive: 3 times, Memoized: 1 time
BNaive: 1 time, Memoized: 1 time
CNaive: 5 times, Memoized: 2 times
DNaive: 3 times, Memoized: 3 times
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calls.