0
0
DSA Cprogramming~20 mins

Recursion Base Case and Recursive Case in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Recursion Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Recursive Factorial Function
What is the output of the following C code when calling factorial(4)?
DSA C
int factorial(int n) {
    if (n == 0) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

int main() {
    int result = factorial(4);
    printf("%d\n", result);
    return 0;
}
A120
B10
C0
D24
Attempts:
2 left
💡 Hint
Remember factorial of 4 is 4 * 3 * 2 * 1.
Predict Output
intermediate
2:00remaining
Output of Recursive Sum Function
What is printed when the following C code runs sum(3)?
DSA C
int sum(int n) {
    if (n == 0) {
        return 0;
    } else {
        return n + sum(n - 1);
    }
}

int main() {
    int result = sum(3);
    printf("%d\n", result);
    return 0;
}
A0
B6
C3
D9
Attempts:
2 left
💡 Hint
Sum of numbers from 1 to 3 is 6.
🔧 Debug
advanced
2:00remaining
Identify the Error in Recursive Fibonacci Function
What error will occur when running this C code?
DSA C
int fibonacci(int n) {
    if (n == 0) {
        return 0;
    } else if (n == 1) {
        return 1;
    } else {
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}

int main() {
    int result = fibonacci(-1);
    printf("%d\n", result);
    return 0;
}
AReturns -1 as output
BReturns 0 without error
CCompilation error due to missing return
DStack overflow due to infinite recursion
Attempts:
2 left
💡 Hint
Check what happens when n is negative.
🧠 Conceptual
advanced
1:30remaining
Base Case Importance in Recursion
Why is the base case essential in a recursive function?
AIt initializes variables for recursion
BIt makes the function run faster
CIt stops the recursion to prevent infinite calls
DIt allows the function to call other functions
Attempts:
2 left
💡 Hint
Think about what happens if recursion never stops.
Predict Output
expert
2:30remaining
Output of Recursive String Length Function
What is the output of this C code when calling strlen_recursive("abc")?
DSA C
#include <stdio.h>

int strlen_recursive(const char *str) {
    if (*str == '\0') {
        return 0;
    } else {
        return 1 + strlen_recursive(str + 1);
    }
}

int main() {
    int length = strlen_recursive("abc");
    printf("%d\n", length);
    return 0;
}
A3
B4
C0
DSegmentation fault
Attempts:
2 left
💡 Hint
Count characters until the null character '\0'.