Factorial Using Recursion in DSA C++ - Time & Space Complexity
We want to understand how the time needed to calculate a factorial grows as the input number gets bigger.
Specifically, how many steps does the recursive factorial function take as the input increases?
Analyze the time complexity of the following code snippet.
int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
This code calculates the factorial of a number n by calling itself with smaller values until it reaches 1.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call to factorial with n-1.
- How many times: Exactly n times until reaching the base case.
Each increase in n adds one more recursive call, so the steps grow directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calls |
| 100 | About 100 calls |
| 1000 | About 1000 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 call reduces n by 1 and only calls once, so calls grow linearly, not exponentially.
Understanding this simple recursion time helps build confidence for more complex recursive problems in interviews.
"What if we changed the recursion to a loop? 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
Only The product of all positive integers up to that number correctly describes this product, others describe sum or unrelated operations.Final Answer:
The product of all positive integers up to that number -> Option AQuick Check:
Factorial = product of integers [OK]
- Confusing factorial with sum
- Thinking factorial divides or doubles the number
- Mixing factorial with addition
Solution
Step 1: Identify factorial base case
By definition, 0! = 1, so the base case must return 1 when n is 0.Step 2: Check options
Only if (n == 0) return 1; returns 1 when n is 0, others return incorrect values.Final Answer:
if (n == 0) return 1; -> Option DQuick Check:
Base case 0! = 1 [OK]
- Returning 0 for base case
- Using n == 1 but returning wrong value
- Missing base case causing infinite recursion
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
std::cout << factorial(4);
return 0;
}Solution
Step 1: Trace recursive calls for factorial(4)
factorial(4) calls 4 * factorial(3), factorial(3) calls 3 * factorial(2), factorial(2) calls 2 * factorial(1), factorial(1) calls 1 * factorial(0), factorial(0) returns 1.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 = 24.Final Answer:
24 -> Option BQuick Check:
4! = 24 [OK]
- Confusing factorial with sum
- Forgetting base case returns 1
- Incorrect multiplication order
int factorial(int n) {
if (n == 1) return 1;
return n * factorial(n + 1);
}Solution
Step 1: Analyze recursive call
The function calls factorial(n + 1), which increases n each time, causing infinite recursion.Step 2: Correct recursive call
To reduce n towards base case, it should call factorial(n - 1) instead.Final Answer:
The recursive call should use n - 1, not n + 1 -> Option CQuick Check:
Recursive call must decrease n [OK]
- Increasing n in recursive call causing infinite loop
- Wrong base case value
- Using addition instead of multiplication
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}What is the output of
factorial(-3) and why?Solution
Step 1: Check base case condition
The base case triggers if n <= 1, so for n = -3, it returns 1 immediately.Step 2: Understand recursion behavior
Since base case covers negative inputs, no recursion occurs and function returns 1 safely.Final Answer:
1, because base case handles n <= 1 -> Option AQuick Check:
Base case covers negatives, returns 1 [OK]
- Assuming negative input causes infinite recursion
- Expecting error or zero for negative input
- Ignoring base case condition
Ready to prove it?
Pass the test and this pattern moves unknown
