0
0
DSA Cprogramming~20 mins

Why Recursion Exists and What Loops Cannot Express Cleanly in DSA C - Challenge Your Understanding

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 vs Loop Factorial
What is the output of the following C code that calculates factorial using recursion and a loop?
DSA C
#include <stdio.h>

int factorial_recursive(int n) {
    if (n <= 1) return 1;
    return n * factorial_recursive(n - 1);
}

int factorial_loop(int n) {
    int result = 1;
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    return result;
}

int main() {
    int n = 4;
    printf("%d %d\n", factorial_recursive(n), factorial_loop(n));
    return 0;
}
A120 120
B24 120
C24 24
D120 24
Attempts:
2 left
💡 Hint
Trace factorial of 4: 4*3*2*1 = 24
🧠 Conceptual
intermediate
1:30remaining
Why Recursion Can Express Tree Traversals Cleanly
Which of the following best explains why recursion is preferred over loops for tree traversals?
ARecursion naturally handles branching structures by calling itself for each child node, which loops cannot do cleanly without extra data structures.
BLoops are faster and use less memory than recursion for tree traversals.
CLoops can only traverse linear data structures, not trees.
DRecursion uses iteration internally, so it is just a fancy loop.
Attempts:
2 left
💡 Hint
Think about how trees have multiple branches at each node.
🔧 Debug
advanced
2:00remaining
Identify the Error in Recursive Fibonacci Implementation
What error will the following C code produce when 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 - 2);
}

int main() {
    int result = fib(-1);
    printf("%d\n", result);
    return 0;
}
ACompilation error
BPrints 0
CPrints 1
DStack overflow due to infinite recursion
Attempts:
2 left
💡 Hint
Consider what happens when n is negative.
🚀 Application
advanced
2:30remaining
Using Recursion to Generate All Subsets
Which output correctly shows all subsets of {1, 2} generated by this recursive function?
DSA C
#include <stdio.h>

void subsets(int arr[], int n, int index, int subset[], int subset_size) {
    if (index == n) {
        printf("{");
        for (int i = 0; i < subset_size; i++) {
            printf("%d", subset[i]);
            if (i < subset_size - 1) printf(", ");
        }
        printf("}\n");
        return;
    }
    // Include current element
    subset[subset_size] = arr[index];
    subsets(arr, n, index + 1, subset, subset_size + 1);
    // Exclude current element
    subsets(arr, n, index + 1, subset, subset_size);
}

int main() {
    int arr[] = {1, 2};
    int subset[2];
    subsets(arr, 2, 0, subset, 0);
    return 0;
}
A{1, 2} {1} {2} {}
B{1} {2} {1, 2} {}
C{} {1} {2} {1, 2}
D{} {2} {1} {1, 2}
Attempts:
2 left
💡 Hint
Full set first (include-include), then {1} (include-exclude), {2} (exclude-include), {} (exclude-exclude).
🧠 Conceptual
expert
1:30remaining
Why Some Problems Cannot Be Expressed Cleanly with Loops Alone
Which statement best explains why certain problems require recursion instead of loops?
ALoops are always slower and less memory efficient than recursion.
BSome problems have self-similar structures that require the function to call itself with smaller inputs, which loops cannot represent without extra data structures.
CLoops cannot perform repeated actions, only recursion can.
DRecursion is the only way to write any algorithm in a functional style.
Attempts:
2 left
💡 Hint
Think about problems like traversing nested folders or trees.