Bird
Raised Fist0
DSA Pythonprogramming~5 mins

Factorial Using Recursion in DSA Python - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Factorial Using Recursion
O(n)
Understanding Time Complexity

We want to understand how the time taken to calculate factorial grows as the input number increases.

Specifically, how many steps does the recursive factorial function take for a given number?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

This code calculates the factorial of a number n using recursion by multiplying n with factorial of n-1 until it reaches 0.

Identify Repeating Operations

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
How Execution Grows With Input

Each increase in n adds one more recursive call, so the steps grow directly with n.

Input Size (n)Approx. Operations
10About 10 recursive calls
100About 100 recursive calls
1000About 1000 recursive calls

Pattern observation: The number of steps increases linearly as n increases.

Final Time Complexity

Time Complexity: O(n)

This means the time to compute factorial grows in a straight line with the input number.

Common Mistake

[X] Wrong: "Recursion makes factorial calculation take exponential time because it calls itself repeatedly."

[OK] Correct: Each recursive call reduces n by 1 and only happens once per call, so calls grow linearly, not exponentially.

Interview Connect

Understanding recursion time complexity helps you explain and optimize recursive solutions clearly in interviews.

Self-Check

"What if we changed the recursion to use memoization? How would the time complexity change?"

Practice

(1/5)
1. What does the factorial of a number represent in mathematics?
easy
A. The product of all positive integers up to that number
B. The sum of all positive integers up to that number
C. The number multiplied by 2
D. The number divided by 2

Solution

  1. Step 1: Understand factorial definition

    Factorial of a number n (written as n!) is the product of all positive integers from 1 to n.
  2. Step 2: Compare options with definition

    Only The product of all positive integers up to that number correctly states this product definition.
  3. Final Answer:

    The product of all positive integers up to that number -> Option A
  4. Quick Check:

    Factorial = product sequence [OK]
Hint: Factorial multiplies numbers down to 1 [OK]
Common Mistakes:
  • Confusing factorial with sum
  • Thinking factorial doubles the number
  • Mixing factorial with division
2. Which of the following is the correct base case for a recursive factorial function in Python?
easy
A. if n == 1: return n + 1
B. if n == 1: return 0
C. if n == 0: return 0
D. if n == 0: return 1

Solution

  1. Step 1: Identify base case for factorial

    Factorial of 0 is defined as 1, so base case should return 1 when n is 0.
  2. Step 2: Check options for correct base case

    if n == 0: return 1 returns 1 when n == 0, which is correct. Others return wrong values.
  3. Final Answer:

    if n == 0: return 1 -> Option D
  4. Quick Check:

    Base case n=0 returns 1 [OK]
Hint: Base case factorial(0) = 1 [OK]
Common Mistakes:
  • Returning 0 for factorial(0)
  • Using wrong base case condition
  • Returning n+1 instead of 1
3. What is the output of the following code?
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

print(factorial(4))
medium
A. 10
B. 16
C. 24
D. 120

Solution

  1. 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)
  2. 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
  3. Final Answer:

    24 -> Option C
  4. Quick Check:

    4! = 24 [OK]
Hint: Multiply down from n to 1 recursively [OK]
Common Mistakes:
  • Stopping recursion too early
  • Adding instead of multiplying
  • Wrong base case causing infinite recursion
4. Find the error in this recursive factorial function:
def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n+1)

print(factorial(3))
medium
A. The recursive call should use n-1, not n+1
B. The print statement should be outside the function
C. The function should return n + factorial(n-1)
D. The base case should be n == 0, not n == 1

Solution

  1. Step 1: Analyze recursive call parameter

    The function calls factorial(n+1), which increases n each time, causing infinite recursion.
  2. Step 2: Correct recursive call to decrement n

    It should call factorial(n-1) to move towards base case and stop recursion.
  3. Final Answer:

    The recursive call should use n-1, not n+1 -> Option A
  4. Quick Check:

    Recursion must approach base case by decreasing n [OK]
Hint: Recursive calls must reduce n to reach base case [OK]
Common Mistakes:
  • Increasing n in recursive call causing infinite loop
  • Wrong base case value
  • Adding instead of multiplying in return
5. How can you modify the recursive factorial function to handle negative inputs gracefully?
hard
A. Change base case to n == -1
B. Add a check to return None or error if n < 0 before recursion
C. Use absolute value of n in recursive call
D. Ignore negative inputs and proceed as usual

Solution

  1. Step 1: Understand factorial domain

    Factorial is not defined for negative numbers, so function should handle this case explicitly.
  2. Step 2: Add input validation before recursion

    Check if n < 0 and return None or raise error to avoid invalid recursion.
  3. Final Answer:

    Add a check to return None or error if n < 0 before recursion -> Option B
  4. Quick Check:

    Validate input before recursion [OK]
Hint: Check for negative input before recursion [OK]
Common Mistakes:
  • Using absolute value hides invalid input
  • Changing base case to negative number
  • Ignoring invalid inputs causes wrong results
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard