Factorial Using Recursion in DSA Java - Time & Space Complexity
We want to understand how the time needed to calculate a factorial grows as the input number gets bigger.
How many steps does the computer take when using recursion to find factorial?
Analyze the time complexity of the following code snippet.
public int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
This code calculates the factorial of a number by calling itself with smaller numbers until it reaches zero.
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 0
Each time we call factorial, it does one multiplication and calls itself with a smaller number until zero.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 multiplications and calls |
| 100 | About 100 multiplications and calls |
| 1000 | About 1000 multiplications and calls |
Pattern observation: The number of steps grows directly with the input size.
Time Complexity: O(n)
This means the time to calculate factorial grows in a straight line as the input number grows.
[X] Wrong: "Recursion makes factorial calculation take much longer than a loop."
[OK] Correct: Both recursion and a loop do about the same number of steps here; recursion just calls itself instead of looping.
Understanding how recursion grows with input helps you explain your code clearly and shows you know how to reason about performance.
"What if we used memoization to store results of factorial calls? 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 BQuick Check:
Factorial = product of integers [OK]
- Confusing factorial with sum
- Thinking factorial divides the number
- Assuming factorial doubles the number
Solution
Step 1: Identify factorial base case
Factorial of 0 is defined as 1, so base case returns 1 when n == 0.Step 2: Check options for correct base case
Only if (n == 0) return 1; returns 1 when n == 0, others return incorrect values.Final Answer:
if (n == 0) return 1; -> Option DQuick Check:
Base case n=0 returns 1 [OK]
- Returning 0 for base case
- Using n==1 with wrong return value
- Missing base case causing infinite recursion
public class Main {
public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
System.out.println(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.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 AQuick Check:
4! = 24 [OK]
- Adding instead of multiplying
- Wrong base case causing wrong output
- Confusing factorial(4) with sum
public static int factorial(int n) {
if (n == 1) return 1;
return n * factorial(n);
}Solution
Step 1: Analyze recursive call parameter
The function calls factorial(n) again without reducing n, causing infinite recursion.Step 2: Identify correct recursive call
Recursive call should be factorial(n - 1) to reduce n and reach base case.Final Answer:
Recursive call uses same n causing infinite recursion -> Option CQuick Check:
Recursive call must reduce n [OK]
- Calling factorial(n) instead of factorial(n-1)
- Missing base case for n=0
- Wrong return type
public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}Solution
Step 1: Identify handling of negative inputs
Factorial is undefined for negative numbers, so function should return -1 for n < 0.Step 2: Add condition before base case
Insert if (n < 0) return -1; before checking base case to catch negatives early.Final Answer:
Add if (n < 0) return -1; before base case -> Option AQuick Check:
Negative input returns -1 [OK]
- Changing base case incorrectly
- Not handling negatives causing infinite recursion
- Assuming factorial works for negatives
Ready to prove it?
Pass the test and this pattern moves unknown
