Recursion vs Iteration When Each Wins in DSA C++ - Complexity Comparison
We want to understand when using recursion or iteration affects how long a program takes to run.
Which method grows faster in time as input size increases?
Analyze the time complexity of these two ways to compute factorial.
// Recursive factorial
int factorialRec(int n) {
if (n <= 1) return 1;
return n * factorialRec(n - 1);
}
// Iterative factorial
int factorialIter(int n) {
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
Both calculate factorial but use recursion and iteration respectively.
Look at what repeats in each method.
- Primary operation: Multiplying numbers from n down to 1.
- How many times: Exactly n times in both recursion and iteration.
As n grows, both methods do more multiplications.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 multiplications |
| 100 | 100 multiplications |
| 1000 | 1000 multiplications |
Pattern observation: The number of steps grows directly with n in both methods.
Time Complexity: O(n)
This means the time to finish grows in a straight line as input size grows.
[X] Wrong: "Recursion is always slower and less efficient than iteration."
[OK] Correct: Both can have the same time complexity; recursion may use more memory but not necessarily more time.
Knowing when recursion or iteration fits best shows you understand how code runs and uses resources, a skill interviewers value.
"What if the recursive function used memoization? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand recursion use case
Recursion works well when a problem can be broken down into smaller versions of itself, like tree traversals or factorial calculation.Step 2: Compare with iteration scenarios
Iteration is better for simple repeated tasks without nested subproblems, so recursion is preferred for divide-and-conquer problems.Final Answer:
When the problem can be divided into similar smaller subproblems -> Option AQuick Check:
Recursion fits divide-and-conquer problems [OK]
- Thinking iteration is always better
- Ignoring stack overflow risk in recursion
- Confusing recursion with simple loops
Solution
Step 1: Identify base case in recursion
Recursive functions must have a condition to stop calling themselves, usually an if statement.Step 2: Check options for base case usage
void func() { if(condition) func(); } uses if(condition) to control recursion, preventing infinite calls. Others cause infinite recursion or loops.Final Answer:
void func() { if(condition) func(); } -> Option AQuick Check:
Recursion needs a stopping condition [OK]
- Writing recursion without base case
- Using loops inside recursion incorrectly
- Confusing infinite loops with recursion
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
std::cout << factorial(4);
return 0;
}Solution
Step 1: Trace factorial calls
factorial(4) calls factorial(3), factorial(2), factorial(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, factorial(4)=4*6=24.Final Answer:
24 -> Option DQuick Check:
4! = 24 [OK]
- Confusing factorial with sum
- Missing base case causing infinite recursion
- Incorrect multiplication order
int sum(int n) {
if (n == 1) return 1;
return n + sum(n);
}Solution
Step 1: Check base case correctness
The base case is correct: if n == 1, return 1.Step 2: Analyze recursive call argument
The recursive call is sum(n), which does not reduce n, so it calls itself infinitely without progress.Final Answer:
Recursive call does not reduce n, causing infinite recursion -> Option BQuick Check:
Recursive calls must reduce problem size [OK]
- Not reducing argument in recursion
- Confusing base case with recursive step
- Assuming recursion always works without checks
A recursive function that processes one element and calls itself for the rest,
or an iterative loop that processes elements one by one?
Solution
Step 1: Understand recursion limits
Recursion uses the call stack for each call, which is limited and can overflow with millions of calls.Step 2: Compare iteration benefits
Iteration uses a single loop and constant stack space, making it safer and more efficient for large datasets.Final Answer:
Iterative approach, because recursion risks stack overflow with large data -> Option CQuick Check:
Iteration avoids stack overflow in large data [OK]
- Assuming recursion is always simpler
- Ignoring stack size limits
- Thinking recursion uses less memory
Ready to prove it?
Pass the test and this pattern moves unknown
