Challenge - 5 Problems
Factorial Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Factorial Call
What is the output of this C# code that calculates factorial recursively?
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 factorial of 4 is 4 * 3 * 2 * 1.
✗ Incorrect
The factorial function multiplies n by factorial of n-1 until n is 0. Factorial(4) = 4*3*2*1 = 24.
❓ Predict Output
intermediateResult of Factorial with Input Zero
What does this recursive factorial function return when input is zero?
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(0)); } }
Attempts:
2 left
💡 Hint
Factorial of zero is defined as 1.
✗ Incorrect
By definition, factorial of 0 is 1. The base case returns 1 immediately.
🔧 Debug
advancedIdentify the Error in Recursive Factorial
What error will this code produce when run?
DSA C#
using System; class Program { static int Factorial(int n) { if (n == 1) return 1; return n * Factorial(n - 1); } static void Main() { Console.WriteLine(Factorial(0)); } }
Attempts:
2 left
💡 Hint
Check the base case and input value.
✗ Incorrect
The base case only stops recursion at n==1. Calling Factorial(0) causes infinite recursion leading to StackOverflowException.
🧠 Conceptual
advancedWhy Use Recursion for Factorial?
Which reason best explains why recursion is a good fit for factorial calculation?
Attempts:
2 left
💡 Hint
Think about how factorial is mathematically defined.
✗ Incorrect
Factorial(n) = n * factorial(n-1) is a natural recursive definition, making recursion intuitive and clear.
❓ Predict Output
expertOutput of Factorial with Negative Input
What happens when this recursive factorial function is called with a negative number?
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(-1)); } }
Attempts:
2 left
💡 Hint
Consider what happens to n in each recursive call.
✗ Incorrect
The base case only stops at n==0. For negative input, n decreases indefinitely causing infinite recursion and StackOverflowException.
