Recursion Base Case and Recursive Case in DSA Python - Time & Space Complexity
When using recursion, it is important to understand how many times the function calls itself.
We want to know how the number of calls grows as the input gets bigger.
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 using recursion with a base case and a recursive case.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The recursive call to
factorial(n - 1). - How many times: It repeats once for each number from
ndown to 1, sontimes.
Each time the function calls itself with a smaller number until it reaches zero.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 recursive calls |
| 100 | About 100 recursive calls |
| 1000 | About 1000 recursive calls |
Pattern observation: The number of calls grows directly with the input size.
Time Complexity: O(n)
This means the time taken grows in a straight line as the input number gets bigger.
[X] Wrong: "Recursion always takes exponential time because it calls itself multiple times."
[OK] Correct: In this example, the function calls itself only once per step, so the time grows linearly, not exponentially.
Understanding how base and recursive cases affect time helps you explain your code clearly and shows you know how recursion works efficiently.
"What if the recursive case called the function twice instead of once? How would the time complexity change?"
Practice
base case in a recursive function?Solution
Step 1: Understand recursion basics
Recursion works by calling itself repeatedly until a stopping condition is met.Step 2: Identify the role of base case
The base case stops further recursive calls to avoid infinite loops.Final Answer:
To stop the recursion and prevent infinite calls -> Option AQuick Check:
Base case = stop recursion [OK]
- Confusing base case with recursive case
- Thinking base case calls recursion again
- Ignoring the need for a stopping condition
Solution
Step 1: Check base case correctness
def fact(n): if n == 0: return 1 else: return n * fact(n-1) usesif n == 0: return 1which is correct for factorial base case.Step 2: Check recursive call correctness
def fact(n): if n == 0: return 1 else: return n * fact(n-1) callsfact(n-1)and multiplies byn, which matches factorial definition.Final Answer:
def fact(n): if n == 0: return 1 else: return n * fact(n-1) -> Option DQuick Check:
Correct base and recursive case = def fact(n): if n == 0: return 1 else: return n * fact(n-1) [OK]
- Using while loop instead of recursion
- Wrong base case value
- Recursive call increasing n instead of decreasing
def countdown(n):
if n == 0:
return 'Done'
else:
print(n)
return countdown(n-1)
countdown(3)Solution
Step 1: Trace the recursive calls and prints
Function prints 3, then calls countdown(2), prints 2, calls countdown(1), prints 1, calls countdown(0).Step 2: Base case returns 'Done'
When n == 0, function returns 'Done' without printing 0.Final Answer:
3 2 1 -> Option BQuick Check:
Prints 3,2,1; base case returns 'Done' (not printed) [OK]
- Expecting 0 to be printed
- Ignoring the need for a stopping condition
- Confusing print order
def sum_n(n):
if n == 1:
return 1
else:
return n + sum_n(n+1)Solution
Step 1: Analyze recursive call parameter
The function callssum_n(n+1), which increases n, moving away from base case.Step 2: Understand effect on recursion
Since n increases, base casen == 1is never reached, causing infinite recursion.Final Answer:
Recursive call increases n causing infinite recursion -> Option CQuick Check:
Recursive call must reduce n to reach base case [OK]
- Increasing n instead of decreasing
- Wrong base case condition
- Ignoring infinite recursion risk
mystery(4)? Explain why.def mystery(n):
if n <= 0:
return 0
else:
return n + mystery(n-2)Solution
Step 1: Trace recursive calls with n=4
mystery(4) calls 4 + mystery(2), mystery(2) calls 2 + mystery(0), mystery(0) returns 0.Step 2: Calculate sum from calls
Sum is 4 + 2 + 0 = 6.Final Answer:
6 (4 + 2 + 0) -> Option AQuick Check:
Recursive case adds n and calls with n-2 until n ≤ 0 [OK]
- Assuming all numbers from n down to 1 are added
- Missing base case for n ≤ 0
- Confusing decrement step
Ready to prove it?
Pass the test and this pattern moves unknown
