Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the sum of elements in the array recursively.
DSA C++
int sumArray(int arr[], int n) {
if (n == 0) return 0;
return arr[n-1] + [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using n+1 causes infinite recursion.
Using n causes no change in problem size.
✗ Incorrect
The function calls itself with n-1 to sum all elements up to index n-1.
2fill in blank
mediumComplete the code to check if a string is a palindrome using recursion.
DSA C++
bool isPalindrome(const string &s, int start, int end) {
if (start >= end) return true;
if (s[start] != s[end]) return false;
return [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Moving only one pointer causes incorrect checks.
Moving pointers in wrong directions causes errors.
✗ Incorrect
The function checks the next inner characters by moving start forward and end backward.
3fill in blank
hardFix the error in the recursive function to find the maximum element in an array.
DSA C++
int maxElement(int arr[], int n) {
if (n == 1) return arr[0];
int max_rest = maxElement(arr, [1]);
return (arr[n-1] > max_rest) ? arr[n-1] : max_rest;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using n+1 causes out-of-bounds access.
Using 0 or 1 does not reduce the problem correctly.
✗ Incorrect
The recursive call should consider the first n-1 elements to find the max.
4fill in blank
hardFill both blanks to create a recursive function that counts occurrences of a character in a string.
DSA C++
int countChar(const string &s, char c, int index) {
if (index == [1]) return 0;
int count_rest = countChar(s, c, index + 1);
return (s[index] == [2]) ? 1 + count_rest : count_rest;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using index instead of string length in base case.
Comparing with wrong character variable.
✗ Incorrect
The base case checks if index reached string length, and the character compared is c.
5fill in blank
hardFill all three blanks to implement a recursive function that reverses a string in place.
DSA C++
void reverseString(string &s, int start, int end) {
if (start >= [1]) return;
swap(s[[2]], s[[3]]);
reverseString(s, start + 1, end - 1);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices for swap.
Incorrect base case condition.
✗ Incorrect
The base case stops when start meets or passes end; swap characters at start and end indices.
