Challenge - 5 Problems
Recursion Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Sum of Array Elements
What is the output of the following code when called with arr = [1, 2, 3, 4] and index = 0?
DSA Python
def recursive_sum(arr, index): if index == len(arr): return 0 return arr[index] + recursive_sum(arr, index + 1) print(recursive_sum([1, 2, 3, 4], 0))
Attempts:
2 left
💡 Hint
Think about how the function adds each element starting from index 0 until the end.
✗ Incorrect
The function adds elements from index 0 to the end recursively. 1+2+3+4 = 10.
❓ Predict Output
intermediateOutput of Recursive String Reverse
What is the output of the following code when called with s = 'code'?
DSA Python
def reverse_string(s): if s == '': return '' return reverse_string(s[1:]) + s[0] print(reverse_string('code'))
Attempts:
2 left
💡 Hint
The function moves the first character to the end recursively.
✗ Incorrect
The function calls itself with the string except the first character, then adds the first character at the end, effectively reversing the string.
🧠 Conceptual
advancedRecursion Depth in Array Search
Consider a recursive function that searches for a target value in an array by checking one element per call. If the array has 1000 elements, what is the maximum recursion depth reached during the search?
Attempts:
2 left
💡 Hint
Each recursive call checks one element and moves to the next.
✗ Incorrect
The recursion depth equals the number of elements checked in the worst case, which is 1000 for 1000 elements.
❓ Predict Output
advancedOutput of Recursive Palindrome Check
What is the output of the following code when called with s = 'racecar'?
DSA Python
def is_palindrome(s, start, end): if start >= end: return True if s[start] != s[end]: return False return is_palindrome(s, start + 1, end - 1) print(is_palindrome('racecar', 0, len('racecar') - 1))
Attempts:
2 left
💡 Hint
Check characters from both ends moving inward.
✗ Incorrect
The function compares characters from start and end indices moving inward. 'racecar' is a palindrome, so it returns True.
🔧 Debug
expertIdentify the Error in Recursive Fibonacci
What error does the following code produce when calling fibonacci(5)?
DSA Python
def fibonacci(n): if n == 0: return 0 elif n == 1: return 1 else: return fibonacci(n - 1) + fibonacci(n - 3) print(fibonacci(5))
Attempts:
2 left
💡 Hint
Check the recursive calls and base cases carefully.
✗ Incorrect
The function calls fibonacci(n - 3) without a base case for n < 0, causing infinite recursion and RecursionError.
