0
0
DSA Cprogramming~10 mins

Recursion vs Iteration When Each Wins in DSA C - Interactive Comparison Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the recursive function call itself.

DSA C
void countdown(int n) {
    if (n <= 0) {
        printf("Blastoff!\n");
        return;
    }
    printf("%d\n", n);
    countdown([1]);
}
Drag options to blanks, or click blank then click option'
An * 2
Bn + 1
Cn - 1
Dn / 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n * 2 or n / 2 does not guarantee reaching zero.
2fill in blank
medium

Complete the iterative loop to count down from n to 1.

DSA C
void countdown_iter(int n) {
    for (int i = n; i > 0; [1]) {
        printf("%d\n", i);
    }
    printf("Blastoff!\n");
}
Drag options to blanks, or click blank then click option'
Ai++
Bi--
Ci = i - 2
Di = i + 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using i++ causes an infinite loop.
Skipping by 2 may miss numbers.
3fill in blank
hard

Fix the error in the recursive factorial function call.

DSA C
int factorial(int n) {
    if (n == 0) return 1;
    return n * factorial([1]);
}
Drag options to blanks, or click blank then click option'
An - 1
Bn + 1
Cn
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n or 1 does not reduce the problem.
4fill in blank
hard

Fill both blanks to create a recursive Fibonacci function.

DSA C
int fibonacci(int n) {
    if (n <= 1) return n;
    return fibonacci([1]) + fibonacci([2]);
}
Drag options to blanks, or click blank then click option'
An - 1
Bn + 1
Cn - 2
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n or wrong subtraction breaks the sequence.
5fill in blank
hard

Fill all three blanks to create an iterative Fibonacci sequence generator.

DSA C
void fibonacci_iter(int n) {
    int a = 0, b = 1, c;
    if (n == 0) {
        printf("%d\n", a);
        return;
    }
    printf("%d\n%d\n", a, b);
    for (int i = 2; i < [1]; [2]) {
        c = a + b;
        printf("%d\n", c);
        a = [3];
        b = c;
    }
}
Drag options to blanks, or click blank then click option'
An
Bi++
Cb
Da + b
Attempts:
3 left
💡 Hint
Common Mistakes
Wrong loop condition causes incorrect sequence length.
Incorrect increment causes infinite loop.
Wrong variable update breaks sequence.