Challenge - 5 Problems
Factorial Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Factorial Function Call
What is the output of the following Python code that calculates factorial recursively?
DSA Python
def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) print(factorial(4))
Attempts:
2 left
💡 Hint
Remember factorial of 4 is 4 * 3 * 2 * 1.
✗ Incorrect
The factorial function multiplies n by factorial of n-1 until it reaches 0, where it returns 1. So factorial(4) = 4*3*2*1 = 24.
❓ Predict Output
intermediateOutput When Calling Factorial with Zero
What will be printed when the factorial function is called with 0?
DSA Python
def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) print(factorial(0))
Attempts:
2 left
💡 Hint
Factorial of zero is defined as 1.
✗ Incorrect
By definition, factorial(0) returns 1 as the base case in recursion.
🔧 Debug
advancedIdentify the Error in Recursive Factorial Code
What error will this code produce when calling factorial(3)?
DSA Python
def factorial(n): if n == 0: return 1 else: return n * factorial(n + 1) print(factorial(3))
Attempts:
2 left
💡 Hint
Check how the recursive call changes the argument.
✗ Incorrect
The recursive call increases n instead of decreasing it, causing infinite recursion until Python stops it.
❓ Predict Output
advancedOutput of Factorial with Negative Input
What happens when factorial is called with a negative number?
DSA Python
def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) print(factorial(-1))
Attempts:
2 left
💡 Hint
Think about the base case and how negative numbers affect recursion.
✗ Incorrect
Since n never reaches 0 when starting negative and decreasing, recursion never stops causing RecursionError.
🧠 Conceptual
expertWhy is a Base Case Necessary in Recursive Factorial?
Why must a recursive factorial function have a base case like 'if n == 0: return 1'?
Attempts:
2 left
💡 Hint
Think about what happens if recursion never stops.
✗ Incorrect
The base case stops recursion by providing a simple answer, preventing infinite calls and errors.
