Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to make the function call itself recursively.
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
Calling countdown with n + 1 causes infinite recursion.
Calling countdown with n does not reduce the problem size.
✗ Incorrect
The function calls itself with n - 1 to count down until it reaches zero.
2fill in blank
mediumComplete the code to calculate factorial of n recursively.
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 1 or 0 directly does not compute factorial correctly.
✗ Incorrect
Factorial of n is n times factorial of n-1, moving towards base case 0.
3fill in blank
hardFix the error in the recursive Fibonacci function call.
DSA C
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci([1]) + fibonacci(n - 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 2 as argument breaks the Fibonacci logic.
✗ Incorrect
Fibonacci is sum of fibonacci(n-1) and fibonacci(n-2).
4fill in blank
hardFill both blanks to complete the recursive sum of array elements.
DSA C
int sumArray(int arr[], int size) {
if (size == 0) {
return 0;
}
return arr[[1]] + sumArray(arr, [2]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using size or size + 1 causes out-of-bounds errors.
Not reducing size leads to infinite recursion.
✗ Incorrect
We add the last element arr[size-1] and sum the rest with size-1.
5fill in blank
hardFill all three blanks to complete the recursive power function.
DSA C
int power(int base, int exp) {
if (exp == 0) {
return [1];
}
return base * power(base, [2]);
}
// Usage: int result = power([3], 3); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning base instead of 1 in base case.
Not reducing exponent causes infinite recursion.
Using wrong base value in usage example.
✗ Incorrect
Base case returns 1 for exponent 0; recursive call reduces exponent by 1; usage example uses base.