Factorial Using Recursion in DSA Python - Time & Space Complexity
We want to understand how the time taken to calculate factorial grows as the input number increases.
Specifically, how many steps does the recursive factorial function take for a given number?
Analyze the time complexity of the following code snippet.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
This code calculates the factorial of a number n using recursion by multiplying n with factorial of n-1 until it reaches 0.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call to factorial with n-1
- How many times: Exactly n times until base case n=0 is reached
Each increase in n adds one more recursive call, so the steps grow directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 recursive calls |
| 100 | About 100 recursive calls |
| 1000 | About 1000 recursive calls |
Pattern observation: The number of steps increases linearly as n increases.
Time Complexity: O(n)
This means the time to compute factorial grows in a straight line with the input number.
[X] Wrong: "Recursion makes factorial calculation take exponential time because it calls itself repeatedly."
[OK] Correct: Each recursive call reduces n by 1 and only happens once per call, so calls grow linearly, not exponentially.
Understanding recursion time complexity helps you explain and optimize recursive solutions clearly in interviews.
"What if we changed the recursion to use memoization? How would the time complexity change?"
Practice
Solution
Step 1: Understand factorial definition
Factorial of a number n (written as n!) is the product of all positive integers from 1 to n.Step 2: Compare options with definition
Only The product of all positive integers up to that number correctly states this product definition.Final Answer:
The product of all positive integers up to that number -> Option AQuick Check:
Factorial = product sequence [OK]
- Confusing factorial with sum
- Thinking factorial doubles the number
- Mixing factorial with division
Solution
Step 1: Identify base case for factorial
Factorial of 0 is defined as 1, so base case should return 1 when n is 0.Step 2: Check options for correct base case
if n == 0: return 1 returns 1 when n == 0, which is correct. Others return wrong values.Final Answer:
if n == 0: return 1 -> Option DQuick Check:
Base case n=0 returns 1 [OK]
- Returning 0 for factorial(0)
- Using wrong base case condition
- Returning n+1 instead of 1
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(4))Solution
Step 1: Trace recursive calls for factorial(4)
factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 * factorial(0) factorial(0) = 1 (base case)Step 2: Calculate values bottom-up
factorial(1) = 1 * 1 = 1 factorial(2) = 2 * 1 = 2 factorial(3) = 3 * 2 = 6 factorial(4) = 4 * 6 = 24Final Answer:
24 -> Option CQuick Check:
4! = 24 [OK]
- Stopping recursion too early
- Adding instead of multiplying
- Wrong base case causing infinite recursion
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n+1)
print(factorial(3))Solution
Step 1: Analyze recursive call parameter
The function calls factorial(n+1), which increases n each time, causing infinite recursion.Step 2: Correct recursive call to decrement n
It should call factorial(n-1) to move towards base case and stop recursion.Final Answer:
The recursive call should use n-1, not n+1 -> Option AQuick Check:
Recursion must approach base case by decreasing n [OK]
- Increasing n in recursive call causing infinite loop
- Wrong base case value
- Adding instead of multiplying in return
Solution
Step 1: Understand factorial domain
Factorial is not defined for negative numbers, so function should handle this case explicitly.Step 2: Add input validation before recursion
Check if n < 0 and return None or raise error to avoid invalid recursion.Final Answer:
Add a check to return None or error if n < 0 before recursion -> Option BQuick Check:
Validate input before recursion [OK]
- Using absolute value hides invalid input
- Changing base case to negative number
- Ignoring invalid inputs causes wrong results
Ready to prove it?
Pass the test and this pattern moves unknown
