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
Remember factorial of 0 is 1 and factorial(n) = n * factorial(n-1).
✗ Incorrect
The function calculates factorial recursively. Factorial(4) = 4 * 3 * 2 * 1 = 24.
❓ Predict Output
intermediateOutput of Recursive Sum Function
What is printed when the following C# code runs with
Sum(3)?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
Sum(n) adds n to Sum(n-1) until n is 0 or less.
✗ Incorrect
Sum(3) = 3 + Sum(2) = 3 + 2 + Sum(1) = 3 + 2 + 1 + Sum(0) = 3 + 2 + 1 + 0 = 6.
❓ Predict Output
advancedOutput of Recursive String Reverse
What is the output of this C# program when calling
Reverse("abc")?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("abc")); } }
Attempts:
2 left
💡 Hint
The function moves the first character to the end recursively.
✗ Incorrect
The function reverses the string by calling itself on the substring without the first character, then appending the first character at the end.
❓ Predict Output
advancedOutput of Recursive Fibonacci Function
What does the following C# code print when calling
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.
🧠 Conceptual
expertIdentifying Base Case in Recursive Function
Which of the following is the correct base case for a recursive function that calculates the sum of numbers from 1 to n?
Attempts:
2 left
💡 Hint
Think about the smallest input where recursion should stop.
✗ Incorrect
The base case for sum from 1 to n is when n is 0, returning 0 stops recursion correctly.
