Factorial Using Recursion in DSA Typescript - Time & Space Complexity
We want to understand how the time to calculate a factorial grows as the 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.
function factorial(n: number): number {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
This code calculates the factorial of a number by calling itself with smaller numbers 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 time we call factorial, it does one multiplication and calls itself with a smaller number.
| 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 n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to compute factorial grows in a straight line as the input number gets bigger.
[X] Wrong: "Recursion makes factorial calculation take much longer than looping."
[OK] Correct: Both recursion and looping here do the same number of steps, so their time grows the same way.
Understanding how recursion affects time helps you explain your code clearly and shows you know how to reason about performance.
"What if we used a loop instead of recursion? 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 factorial as a product, not sum or other 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
- Mixing factorial with doubling
Solution
Step 1: Identify factorial base case
Factorial of 0 is defined as 1, so the base case should return 1 when n is 0.Step 2: Check options
Only if (n === 0) return 1; returns 1 when n is 0, which is correct base case for recursion.Final Answer:
if (n === 0) return 1; -> Option AQuick Check:
Base case: 0! = 1 [OK]
- Returning 0 for base case
- Using wrong base condition
- Returning n+1 incorrectly
function factorial(n: number): number {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(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 DQuick Check:
4! = 24 [OK]
- Stopping recursion too early
- Adding instead of multiplying
- Wrong base case
function factorial(n: number): number {
if (n === 1) return 1;
return n * factorial(n - 1);
}
console.log(factorial(0));Solution
Step 1: Analyze base case for input 0
Base case triggers only when n === 1, so factorial(0) calls factorial(-1), then factorial(-2), and so on infinitely.Step 2: Understand recursion behavior
Without base case for 0, recursion never stops for input 0, causing infinite recursion (stack overflow).Final Answer:
Infinite recursion for input 0 -> Option BQuick Check:
Missing base case for 0 causes infinite recursion [OK]
- Missing base case for 0
- Assuming 1 covers 0
- Ignoring negative inputs
Solution
Step 1: Understand factorial domain
Factorial is not defined for negative numbers, so function should handle negatives before recursion.Step 2: Add input validation
Adding a condition to return -1 (or error) for negative inputs prevents invalid recursion calls.Final Answer:
Add a check to return -1 for negative inputs before recursion -> Option CQuick Check:
Validate input before recursion to handle negatives [OK]
- Ignoring negative inputs
- Letting recursion run on negatives
- Returning wrong values for negatives
Ready to prove it?
Pass the test and this pattern moves unknown
