Challenge - 5 Problems
Recursion Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Sum on Array
What is the output of the following C++ code that recursively sums elements of an array?
DSA C++
#include <iostream> 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); std::cout << result << std::endl; 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 previous elements recursively. For arr = {1,2,3,4}, sum is 1+2+3+4 = 10.
❓ Predict Output
intermediateOutput of Recursive String Reverse
What is the output of this C++ code that recursively reverses a string?
DSA C++
#include <iostream> #include <string> std::string reverseString(const std::string& s) { if (s.empty()) return ""; return reverseString(s.substr(1)) + s[0]; } int main() { std::string input = "code"; std::cout << reverseString(input) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
The function calls itself on the substring excluding the first character, then adds the first character at the end.
✗ Incorrect
The function reverses the string by moving the first character to the end after reversing the rest recursively. "code" reversed is "edoc".
🧠 Conceptual
advancedRecursion Base Case Importance
Why is a base case necessary in a recursive function that processes arrays or strings?
Attempts:
2 left
💡 Hint
Think about what happens if recursion never stops.
✗ Incorrect
The base case stops recursion by providing a condition where the function returns without further calls, preventing infinite recursion.
🔧 Debug
advancedIdentify the Error in Recursive Palindrome Check
What error does this C++ code produce when checking if a string is a palindrome recursively?
DSA C++
#include <iostream> #include <string> bool isPalindrome(const std::string& s, int start, int end) { if (start >= end) return true; if (s[start] != s[end]) return false; return isPalindrome(s, start + 1, end - 1); } int main() { std::string test = "level"; std::cout << isPalindrome(test, 0, test.size() - 1) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check the value of 'end' passed to the function and how it is used as an index.
✗ Incorrect
The function uses 'end' as an index, but main originally passed test.size() which is out of bounds (indexing from 0 to size()-1). Passing test.size() - 1 fixes this and outputs true correctly.
🚀 Application
expertNumber of Recursive Calls in Fibonacci
How many times is the function called (including the first call) when computing fibonacci(4) using this naive recursive code?
DSA C++
#include <iostream> int fibonacci(int n) { if (n <= 1) return n; return fibonacci(n-1) + fibonacci(n-2); } int main() { int calls = fibonacci(4); std::cout << calls << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Draw the call tree for fibonacci(4) and count all calls.
✗ Incorrect
The call tree for fibonacci(4) has 9 calls total: fibonacci(4), fibonacci(3), fibonacci(2), fibonacci(1), fibonacci(0), fibonacci(1), fibonacci(2), fibonacci(1), fibonacci(0).
