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 of Array Elements
What is the output of the following C# code that recursively sums elements of an array?
DSA C#
using System; class Program { static int RecursiveSum(int[] arr, int index) { if (index == arr.Length) return 0; return arr[index] + RecursiveSum(arr, index + 1); } static void Main() { int[] numbers = {1, 2, 3, 4}; Console.WriteLine(RecursiveSum(numbers, 0)); } }
Attempts:
2 left
💡 Hint
Think about how the function adds each element starting from index 0 until the end.
✗ Incorrect
The function adds each element of the array starting from index 0 recursively until it reaches the end, summing 1+2+3+4 = 10.
❓ Predict Output
intermediateOutput of Recursive String Reverse
What is the output of this C# code that reverses a string recursively?
DSA C#
using System; class Program { static string ReverseString(string s) { if (s.Length <= 1) return s; return ReverseString(s.Substring(1)) + s[0]; } static void Main() { string input = "code"; Console.WriteLine(ReverseString(input)); } }
Attempts:
2 left
💡 Hint
The function takes the first character and puts it at the end after reversing the rest.
✗ Incorrect
The function reverses the string by recursively reversing the substring from index 1 and appending the first character at the end.
❓ Predict Output
advancedOutput of Recursive Palindrome Check
What is the output of this C# code that checks if a string is a palindrome using recursion?
DSA C#
using System; class Program { static bool IsPalindrome(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); } static void Main() { string test = "racecar"; Console.WriteLine(IsPalindrome(test, 0, test.Length - 1)); } }
Attempts:
2 left
💡 Hint
A palindrome reads the same forwards and backwards.
✗ Incorrect
The function compares characters from start and end moving inward. Since "racecar" is a palindrome, it returns true.
❓ Predict Output
advancedOutput of Recursive Fibonacci with Memoization
What is the output of this C# code that calculates the 5th Fibonacci number using recursion with memoization?
DSA C#
using System; using System.Collections.Generic; class Program { static Dictionary<int, int> memo = new Dictionary<int, int>(); static int Fibonacci(int n) { if (n <= 1) return n; if (memo.ContainsKey(n)) return memo[n]; memo[n] = Fibonacci(n - 1) + Fibonacci(n - 2); return memo[n]; } static void Main() { Console.WriteLine(Fibonacci(5)); } }
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts 0,1,1,2,3,5,...
✗ Incorrect
The 5th Fibonacci number (0-based) is 5. Memoization avoids repeated calculations.
🧠 Conceptual
expertMaximum Call Stack Depth in Recursive String Palindrome Check
Consider a recursive function that checks if a string of length n is a palindrome by comparing characters from the ends moving inward. What is the maximum depth of the call stack during execution?
Attempts:
2 left
💡 Hint
Each recursive call moves inward by one character from both ends.
✗ Incorrect
Each recursive call reduces the problem size by 2 characters (one from start and one from end), so maximum depth is about n/2.
