Factorial Using Recursion in DSA C - Time & Space Complexity
We want to understand how the time needed to calculate a factorial grows as the number gets bigger.
Specifically, how many steps does the recursive factorial function take as input increases?
Analyze the time complexity of the following code snippet.
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
This code calculates the factorial of a number 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 input 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 grows in a straight line as n increases.
Time Complexity: O(n)
This means the time to compute factorial grows directly in proportion to the input number.
[X] Wrong: "Recursion makes factorial calculation take exponential time because it calls itself many times."
[OK] Correct: Each recursive call happens only once per number down to zero, so calls grow linearly, not exponentially.
Understanding how recursion affects time helps you explain and optimize solutions clearly in interviews.
"What if we changed the recursion to use a loop instead? 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 describes factorial as a product, not sum or division.Final Answer:
The product of all positive integers up to that number -> Option DQuick Check:
Factorial = product of integers [OK]
- Confusing factorial with sum
- Thinking factorial divides the number
- Mixing multiplication with addition
Solution
Step 1: Identify base case for factorial
Factorial of 0 is defined as 1, so base case must return 1 when n is 0.Step 2: Check options for correct base case
if (n == 0) return 1; returns 1 when n is 0, which is correct. Others return wrong values or conditions.Final Answer:
if (n == 0) return 1; -> Option AQuick Check:
Base case n=0 returns 1 [OK]
- Returning 0 for base case
- Using wrong condition like n==1
- Returning n+1 incorrectly
int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
int main() {
printf("%d", factorial(4));
return 0;
}Solution
Step 1: Trace factorial(4) calls
factorial(4) calls factorial(3), factorial(3) calls factorial(2), factorial(2) calls factorial(1), factorial(1) calls factorial(0).Step 2: Calculate return values
factorial(0) = 1
factorial(1) = 1 * 1 = 1
factorial(2) = 2 * 1 = 2
factorial(3) = 3 * 2 = 6
factorial(4) = 4 * 6 = 24Final Answer:
24 -> Option AQuick Check:
4! = 24 [OK]
- Stopping recursion too early
- Adding instead of multiplying
- Wrong base case causing infinite recursion
int factorial(int n) {
if (n == 1) return 1;
else return n * factorial(n);
}Solution
Step 1: Analyze recursive call
The function calls factorial(n) inside itself without reducing n, causing infinite recursion.Step 2: Correct recursive call to reduce n
Changing call to factorial(n - 1) ensures n decreases each call, reaching base case.Final Answer:
Change recursive call to factorial(n - 1); -> Option BQuick Check:
Recursive call must reduce n [OK]
- Calling factorial(n) again causing infinite loop
- Wrong base case value
- Using addition instead of multiplication
Choose the correct code snippet.
Solution
Step 1: Add check for negative input
Factorial is not defined for negative numbers, so return -1 if n < 0.Step 2: Keep base case and recursive call correct
Return 1 if n == 0, else multiply n by factorial(n - 1).Final Answer:
if (n < 0) return -1; else if (n == 0) return 1; else return n * factorial(n - 1); -> Option CQuick Check:
Negative input returns -1 [OK]
- Not handling negative input
- Returning wrong value for negatives
- Incorrect base case for zero
Ready to prove it?
Pass the test and this pattern moves unknown
