Factorial Using Recursion in DSA Go - Time & Space Complexity
We want to understand how the time taken by a recursive factorial function grows as the input number increases.
Specifically, how many steps does the function take to calculate factorial of n?
Analyze the time complexity of the following code snippet.
package main
func factorial(n int) int {
if n == 0 {
return 1
}
return n * factorial(n-1)
}
This code calculates the factorial of a number n using recursion by calling itself with n-1 until it reaches 0.
- 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 calls |
| 100 | About 100 calls |
| 1000 | About 1000 calls |
Pattern observation: The number of steps grows linearly as n increases.
Time Complexity: O(n)
This means the time to compute factorial grows in a straight line with the input size n.
[X] Wrong: "Recursion makes factorial calculation take exponential time because it calls itself many times."
[OK] Correct: Each recursive call happens once per number down to zero, so calls grow linearly, not exponentially.
Understanding recursive time complexity helps you explain how your code scales and shows you can reason about function calls clearly.
"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 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 doubles the number
- Assuming factorial divides the number
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
Only if n == 0 { return 1 } returns 1 when n == 0, which is correct base case.Final Answer:
if n == 0 { return 1 } -> Option AQuick Check:
Base case n=0 returns 1 [OK]
- Returning 0 for base case
- Returning wrong value for n=1
- Missing base case causing infinite recursion
func factorial(n int) int {
if n == 0 {
return 1
}
return n * factorial(n-1)
}
fmt.Println(factorial(3))Solution
Step 1: Trace factorial(3) calls
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 return values
factorial(1) = 1*1=1, factorial(2) = 2*1=2, factorial(3) = 3*2=6.Final Answer:
6 -> Option CQuick Check:
3! = 6 [OK]
- Adding instead of multiplying
- Returning n instead of recursive call
- Missing base case causing stack overflow
func factorial(n int) int {
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 n instead of n-1 causing infinite recursion -> Option BQuick Check:
Recursive call must reduce n [OK]
- Calling factorial with same n causing infinite loop
- Wrong base case value
- Ignoring base case leading to stack overflow
Solution
Step 1: Understand factorial domain
Factorial is not defined for negative numbers, so function should handle negatives explicitly.Step 2: Add input validation
Adding a check to return 0 (or error) if n is negative prevents invalid recursion calls.Final Answer:
Add a check to return 0 if n is negative before recursion -> Option DQuick Check:
Validate input before recursion [OK]
- Ignoring negative inputs causing infinite recursion
- Using absolute value without validation
- Removing base case causing stack overflow
Ready to prove it?
Pass the test and this pattern moves unknown
