Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n * 2 or n / 2 does not guarantee reaching zero.
✗ Incorrect
The recursive call must reduce n by 1 to eventually reach the base case.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i++ causes an infinite loop.
Skipping by 2 may miss numbers.
✗ Incorrect
The loop must decrease i by 1 each time to count down.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n or 1 does not reduce the problem.
✗ Incorrect
Factorial calls itself with n - 1 to reduce the problem size.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n or wrong subtraction breaks the sequence.
✗ Incorrect
Fibonacci is sum of fibonacci(n-1) and fibonacci(n-2).
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Wrong loop condition causes incorrect sequence length.
Incorrect increment causes infinite loop.
Wrong variable update breaks sequence.
✗ Incorrect
Loop runs while i < n, increments i, and updates a to previous b.