0
0
DSA Cprogramming~20 mins

Recursion Concept and Call Stack Visualization 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;
    return n * factorial(n - 1);
}

int main() {
    int result = factorial(4);
    printf("%d", result);
    return 0;
}
A24
B10
C120
D0
Attempts:
2 left
💡 Hint
Recall that factorial of n is n multiplied by factorial of n-1, with factorial(0) = 1.
Predict Output
intermediate
2:00remaining
Output of Recursive Sum Function
What will be printed after executing this C code snippet?
DSA C
int sum(int n) {
    if (n <= 0) return 0;
    return n + sum(n - 1);
}

int main() {
    printf("%d", sum(5));
    return 0;
}
A10
B15
C5
D0
Attempts:
2 left
💡 Hint
Sum of first n natural numbers is n*(n+1)/2.
🔧 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;
    if (n == 1) return 1;
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
    int result = fibonacci(-1);
    printf("%d", result);
    return 0;
}
ACompilation error
BPrints 0
CStack overflow due to infinite recursion
DPrints 1
Attempts:
2 left
💡 Hint
Consider what happens when n is negative in the recursive calls.
🧠 Conceptual
advanced
2:00remaining
Call Stack Depth in Recursive Function
If a recursive function calls itself with n-1 until n reaches 0, what is the maximum depth of the call stack when called with n=7?
A1
B7
C6
D8
Attempts:
2 left
💡 Hint
Count the initial call plus all recursive calls until base case.
🚀 Application
expert
3:00remaining
Output of Recursive String Reverse Function
What is the output of this C program that reverses a string recursively?
DSA C
#include <stdio.h>
#include <string.h>

void reverse(char *str, int start, int end) {
    if (start >= end) return;
    char temp = str[start];
    str[start] = str[end];
    str[end] = temp;
    reverse(str, start + 1, end - 1);
}

int main() {
    char s[] = "hello";
    reverse(s, 0, strlen(s) - 1);
    printf("%s", s);
    return 0;
}
Aolleh
Bhello
Cohlle
Derror
Attempts:
2 left
💡 Hint
The function swaps characters from start and end moving inward.