Challenge - 5 Problems
Recursion Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Factorial Function
What is the output of the following C# code when calling
Factorial(4)?DSA C#
using System; class Program { static int Factorial(int n) { if (n == 0) return 1; return n * Factorial(n - 1); } static void Main() { Console.WriteLine(Factorial(4)); } }
Attempts:
2 left
💡 Hint
Recall that factorial of n is n multiplied by factorial of n-1, with factorial of 0 as 1.
✗ Incorrect
The factorial of 4 is 4 * 3 * 2 * 1 = 24. The recursion multiplies down to 1.
❓ Predict Output
intermediateOutput of Recursive Fibonacci Function
What is printed when the following C# program runs
Fibonacci(5)?DSA C#
using System; class Program { static int Fibonacci(int n) { if (n <= 1) return n; return Fibonacci(n - 1) + Fibonacci(n - 2); } static void Main() { Console.WriteLine(Fibonacci(5)); } }
Attempts:
2 left
💡 Hint
Fibonacci sequence starts 0,1,1,2,3,5,...
✗ Incorrect
Fibonacci(5) = Fibonacci(4) + Fibonacci(3) = 3 + 2 = 5.
🔧 Debug
advancedIdentify the Error in Recursive Sum Function
What error occurs when running this C# code that sums numbers from n down to 1 recursively?
DSA C#
using System; class Program { static int Sum(int n) { if (n == 0) return 0; return n + Sum(n - 1); } static void Main() { Console.WriteLine(Sum(3)); } }
Attempts:
2 left
💡 Hint
Check if the recursive call reduces the problem size.
✗ Incorrect
The original code caused infinite recursion because the recursive call did not reduce n. The fixed code reduces n by 1 each call, so it terminates correctly.
🧠 Conceptual
advancedCall Stack Depth for Recursive Function
If a recursive function calls itself with
n-1 until n == 0, what is the maximum call stack depth when called with n = 7?Attempts:
2 left
💡 Hint
Count the initial call plus each recursive call until base case.
✗ Incorrect
The function calls itself 7 times plus the initial call, totaling 8 calls on the stack.
🚀 Application
expertOutput of Recursive String Reversal
What is the output of this C# program that reverses a string recursively when input is
"code"?DSA C#
using System; class Program { static string Reverse(string s) { if (s.Length == 0) return ""; return Reverse(s.Substring(1)) + s[0]; } static void Main() { Console.WriteLine(Reverse("code")); } }
Attempts:
2 left
💡 Hint
The function moves the first character to the end after reversing the rest.
✗ Incorrect
The recursion reverses the substring from index 1 onward, then adds the first character at the end, effectively reversing the string.
