0
0
DSA Cprogramming~20 mins

Fibonacci Using Recursion in DSA C - Practice Problems & Challenges

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

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

int main() {
    for (int i = 0; i < 6; i++) {
        printf("%d ", fib(i));
    }
    return 0;
}
A0 1 1 2 3 5
B1 1 2 3 5 8
C0 1 2 3 5 8
D1 2 3 5 8 13
Attempts:
2 left
💡 Hint
Remember that fib(0) = 0 and fib(1) = 1 in this code.
Predict Output
intermediate
1:30remaining
Value of fib(4)
What is the value returned by fib(4) in the following recursive Fibonacci function?
DSA C
int fib(int n) {
    if (n <= 1) return n;
    return fib(n - 1) + fib(n - 2);
}
A2
B3
C5
D4
Attempts:
2 left
💡 Hint
Calculate fib(3) + fib(2) to get fib(4).
🔧 Debug
advanced
2:00remaining
Identify the error in Fibonacci recursion
What error will the following code produce when compiled and run?
DSA C
#include <stdio.h>

int fib(int n) {
    if (n == 0) return 0;
    else if (n == 1) return 1;
    else return fib(n - 1) + fib(n - 3);
}

int main() {
    printf("%d", fib(4));
    return 0;
}
AOutput: 5
BOutput: 3
CRuntime error: stack overflow
DCompilation error
Attempts:
2 left
💡 Hint
Check the recursive calls and their arguments.
🧠 Conceptual
advanced
1:30remaining
Time Complexity of Recursive Fibonacci
What is the time complexity of the naive recursive Fibonacci function fib(n) defined as fib(n) = fib(n-1) + fib(n-2) with base cases fib(0)=0 and fib(1)=1?
AO(log n)
BO(n)
CO(n^2)
DO(2^n)
Attempts:
2 left
💡 Hint
Consider how many calls are made for each fib(n).
🚀 Application
expert
2:30remaining
Output of Memoized Fibonacci Calls
Given the following C code implementing Fibonacci with memoization, what is the output printed by the program?
DSA C
#include <stdio.h>

int memo[6] = {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() {
    for (int i = 0; i < 6; i++) {
        printf("%d ", fib(i));
    }
    return 0;
}
A0 1 1 2 3 5
B0 1 2 3 5 8
C1 1 2 3 5 8
D1 2 3 5 8 13
Attempts:
2 left
💡 Hint
Memoization stores previously computed results to avoid repeated work.