Recursion Base Case and Recursive Case in DSA Rust - Time & Space Complexity
When using recursion, it is important to understand how the function repeats itself and when it stops.
We want to know how the number of steps grows as the input gets bigger.
Analyze the time complexity of the following code snippet.
fn factorial(n: u32) -> u32 {
if n == 0 {
1
} else {
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 function calls itself with a smaller number each time.
- How many times: It repeats until the number reaches zero, so n times.
Each call waits for the next call with a smaller number until it reaches zero.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calls |
| 100 | About 100 calls |
| 1000 | About 1000 calls |
Pattern observation: The number of steps grows directly with the input size.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input number gets bigger.
[X] Wrong: "Recursion always means exponential time because it repeats a lot."
[OK] Correct: Here, the recursion only happens once per call, so it 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?Solution
Step 1: Understand the base case role
The base case is the condition that stops the recursion from continuing indefinitely.Step 2: Differentiate from recursive case
The recursive case calls the function again with smaller input, but the base case stops recursion.Final Answer:
To stop the recursion and prevent infinite calls -> Option DQuick Check:
Base case = stop recursion [OK]
- Confusing base case with recursive case
- Thinking base case increases input size
- Assuming base case prints output
Solution
Step 1: Check base case and recursive call
fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } correctly usesif n == 0 { 1 }as base case and recursive multiplication.Step 2: Identify syntax errors in other options
fn factorial(n: u32) -> u32 { while n > 0 { n * factorial(n - 1) } } uses while loop incorrectly, C loops infinitely, D uses addition instead of multiplication.Final Answer:
fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } -> Option CQuick Check:
Correct base case and recursive call = fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } } [OK]
- Using loops instead of recursion
- Missing base case
- Wrong operation in recursive call
fn sum(n: u32) -> u32 {
if n == 0 {
0
} else {
n + sum(n - 1)
}
}
fn main() {
println!("{}", sum(3));
}Solution
Step 1: Trace recursive calls for sum(3)
sum(3) = 3 + sum(2), sum(2) = 2 + sum(1), sum(1) = 1 + sum(0), sum(0) = 0.Step 2: Calculate total sum
sum(3) = 3 + 2 + 1 + 0 = 6.Final Answer:
6 -> Option BQuick Check:
Sum 1 to 3 = 6 [OK]
- Confusing base case value
- Forgetting to add current n
- Expecting runtime error
fn factorial(n: u32) -> u32 {
n * factorial(n - 1)
}
fn main() {
println!("{}", factorial(3));
}Solution
Step 1: Check for base case presence
The function lacks a base case likeif n == 0 { 1 }to stop recursion.Step 2: Understand recursion consequences
Without base case, recursion never stops, causing stack overflow error.Final Answer:
Missing base case to stop recursion -> Option AQuick Check:
Base case missing = Missing base case to stop recursion [OK]
- Assuming recursion stops automatically
- Ignoring base case importance
- Confusing return types
Solution
Step 1: Identify correct base cases for Fibonacci
Fibonacci sequence starts with fib(0) = 0 and fib(1) = 1, so base cases must reflect that.Step 2: Check recursive case correctness
Recursive case sums previous two Fibonacci numbers: fib(n-1) + fib(n-2).Final Answer:
if n == 0 { 0 } else if n == 1 { 1 } else { fib(n - 1) + fib(n - 2) } -> Option AQuick Check:
Correct base and recursive case for Fibonacci = if n == 0 { 0 } else if n == 1 { 1 } else { fib(n - 1) + fib(n - 2) } [OK]
- Wrong base case values
- Using multiplication or subtraction instead of addition
- Missing one base case
Ready to prove it?
Pass the test and this pattern moves unknown
