Bird
Raised Fist0
DSA Cprogramming~20 mins

Fibonacci Using Recursion in DSA C - Practice Problems & Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the Fibonacci sequence represent when calculated using recursion?
easy
A. Each number is the sum of the two preceding numbers
B. Each number is the product of the two preceding numbers
C. Each number is the difference of the two preceding numbers
D. Each number is the division of the two preceding numbers

Solution

  1. Step 1: Understand Fibonacci sequence definition

    The Fibonacci sequence is defined such that each number is the sum of the two numbers before it.
  2. Step 2: Relate to recursion

    Recursion calls the function for the two previous numbers and adds their results to get the current number.
  3. Final Answer:

    Each number is the sum of the two preceding numbers -> Option A
  4. Quick Check:

    Fibonacci = sum of two previous numbers [OK]
Hint: Fibonacci adds previous two numbers recursively [OK]
Common Mistakes:
  • Thinking Fibonacci multiplies previous numbers
  • Confusing subtraction or division with addition
  • Assuming Fibonacci uses only one previous number
2. Which of the following is the correct base case for a recursive Fibonacci function in C?
easy
A. if (n < 2) return 2;
B. if (n == 0) return 0; else return 1;
C. if (n <= 1) return n;
D. if (n == 1) return 0;

Solution

  1. Step 1: Identify base cases for Fibonacci

    Fibonacci sequence starts with 0 and 1 for n=0 and n=1 respectively, so base case returns n when n is 0 or 1.
  2. Step 2: Match base case syntax

    The condition if (n <= 1) return n; correctly returns 0 for n=0 and 1 for n=1.
  3. Final Answer:

    if (n <= 1) return n; -> Option C
  4. Quick Check:

    Base case returns n for n=0 or 1 [OK]
Hint: Base case returns n when n is 0 or 1 [OK]
Common Mistakes:
  • Returning 2 for base case
  • Returning 0 for n=1 incorrectly
  • Missing base case leading to infinite recursion
3. What is the output of the following C code snippet?
int fib(int n) {
  if (n <= 1) return n;
  return fib(n-1) + fib(n-2);
}

int main() {
  printf("%d", fib(4));
  return 0;
}
medium
A. 4
B. 3
C. 5
D. 7

Solution

  1. Step 1: Calculate fib(4) using recursion

    fib(4) = fib(3) + fib(2) - fib(3) = fib(2) + fib(1) = (fib(1)+fib(0)) + 1 = (1+0)+1=2 - fib(2) = fib(1) + fib(0) = 1 + 0 = 1 So fib(4) = 2 + 1 = 3
  2. Step 2: Confirm output printed

    The printf prints the integer 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    fib(4) = 3 [OK]
Hint: Trace fib calls down to base cases [OK]
Common Mistakes:
  • Confusing fib(4) with fib(5)
  • Adding wrong fib values
  • Forgetting base cases
4. Identify the error in this recursive Fibonacci function and select the fix:
int fib(int n) {
  if (n == 0) return 0;
  else return fib(n-1) + fib(n-2);
}
medium
A. Add base case for n == 1 to return 1
B. Change return 0 to return 1 for n == 0
C. Remove else and just return fib(n-1) + fib(n-2)
D. Change function to iterative instead of recursive

Solution

  1. Step 1: Check base cases

    Function only returns 0 for n==0 but misses base case for n==1, causing infinite recursion for n=1.
  2. Step 2: Fix by adding base case for n==1

    Adding if (n == 1) return 1; fixes recursion and returns correct Fibonacci values.
  3. Final Answer:

    Add base case for n == 1 to return 1 -> Option A
  4. Quick Check:

    Both n=0 and n=1 base cases needed [OK]
Hint: Always include base cases for n=0 and n=1 [OK]
Common Mistakes:
  • Missing base case for n=1
  • Changing base case values incorrectly
  • Removing else causing syntax confusion
5. You want to compute the 6th Fibonacci number using recursion but want to avoid repeated calculations. Which approach improves efficiency?
hard
A. Call fib(n-1) twice to ensure correctness
B. Increase the recursion depth limit
C. Use a loop instead of recursion without storing values
D. Use memoization to store and reuse computed Fibonacci values

Solution

  1. Step 1: Understand repeated calculations in recursion

    Recursive Fibonacci calls fib(n-1) and fib(n-2) repeatedly, causing many duplicate calls and inefficiency.
  2. Step 2: Use memoization to store results

    Memoization saves computed Fibonacci values in a cache, so repeated calls return stored results instantly, improving speed.
  3. Final Answer:

    Use memoization to store and reuse computed Fibonacci values -> Option D
  4. Quick Check:

    Memoization avoids repeated work [OK]
Hint: Store results to avoid repeated recursive calls [OK]
Common Mistakes:
  • Increasing recursion depth doesn't fix inefficiency
  • Using loops without caching still repeats work
  • Calling fib(n-1) twice wastes time
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard