Challenge - 5 Problems
Recursion Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Recursive Sum of Array Elements
What is the output of the following C code that recursively sums elements of an array?
DSA C
int recursiveSum(int arr[], int n) { if (n == 0) return 0; return arr[n-1] + recursiveSum(arr, n-1); } int main() { int arr[] = {1, 2, 3, 4}; int result = recursiveSum(arr, 4); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Think about how the function adds the last element and calls itself with one less element.
✗ Incorrect
The function adds the last element arr[n-1] to the sum of the first n-1 elements recursively. For arr = {1,2,3,4}, sum is 1+2+3+4 = 10.
❓ Predict Output
intermediate2:00remaining
Output of Recursive String Length Function
What is the output of this C code that calculates the length of a string recursively?
DSA C
int recursiveLength(char *str) { if (*str == '\0') return 0; return 1 + recursiveLength(str + 1); } int main() { char s[] = "hello"; int len = recursiveLength(s); printf("%d", len); return 0; }
Attempts:
2 left
💡 Hint
Count characters until the null character '\0' is found.
✗ Incorrect
The function counts each character until it reaches the string end '\0'. For "hello", length is 5.
🔧 Debug
advanced2:00remaining
Identify the Error in Recursive Array Search
What error does the following C code produce when searching for a value recursively in an array?
DSA C
int recursiveSearch(int arr[], int n, int x) { if (n < 0) return -1; if (arr[n] == x) return n; return recursiveSearch(arr, n-1, x); } int main() { int arr[] = {1, 3, 5, 7}; int index = recursiveSearch(arr, 4, 5); printf("%d", index); return 0; }
Attempts:
2 left
💡 Hint
Check the array index used in the base case and recursive calls.
✗ Incorrect
The function accesses arr[n] where n starts at 4, but arr has indices 0 to 3. The initial call uses n=4 which is invalid, but the base case checks n<0. However, the function calls arr[n] without adjusting for zero-based indexing properly, causing out-of-bounds access.
❓ Predict Output
advanced2:00remaining
Output of Recursive Palindrome Check
What is the output of this C code that checks if a string is a palindrome recursively?
DSA C
int isPalindrome(char *str, int start, int end) { if (start >= end) return 1; if (str[start] != str[end]) return 0; return isPalindrome(str, start + 1, end - 1); } int main() { char s[] = "racecar"; int result = isPalindrome(s, 0, 6); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Check characters from start and end moving inward.
✗ Incorrect
The function returns 1 if the string is a palindrome. "racecar" reads the same forwards and backwards, so output is 1.
🧠 Conceptual
expert2:00remaining
Maximum Depth of Recursive Calls for String Reverse
Consider a recursive function that reverses a string by swapping characters from start to end indices. What is the maximum depth of recursive calls for a string of length n?
Attempts:
2 left
💡 Hint
Each recursive call processes one pair of characters from start and end.
✗ Incorrect
The function swaps characters at start and end, then calls itself with start+1 and end-1. It stops when start >= end. So the number of calls is about half the string length.