Recursion is when a function calls itself to solve smaller parts of a problem until it reaches a simple case it can solve directly.
Click to reveal answer
beginner
What is iteration in simple terms?
Iteration means repeating a set of instructions using loops like 'for' or 'while' until a condition is met.
Click to reveal answer
intermediate
When is recursion better than iteration?
Recursion is better when the problem is naturally divided into similar smaller problems, like tree traversals or divide-and-conquer algorithms.
Click to reveal answer
intermediate
When is iteration better than recursion?
Iteration is better when you want to save memory and avoid the overhead of many function calls, especially for simple loops or when recursion depth can be very large.
Click to reveal answer
beginner
What is a risk of using recursion without care?
Using recursion without a proper base case or with too deep calls can cause a stack overflow error, crashing the program.
Click to reveal answer
Which method is usually easier to write for problems like tree traversal?
ARecursion
BIteration
CBoth are equally easy
DNeither works
✗ Incorrect
Recursion naturally fits tree traversal because each node can be processed by calling the same function on its children.
What is a common disadvantage of recursion compared to iteration?
AUses more memory due to function call stack
BCannot solve complex problems
CRuns slower because loops are not allowed
DAlways harder to understand
✗ Incorrect
Recursion uses extra memory for each function call on the call stack, which iteration avoids.
Which approach is better for simple repeated tasks like summing numbers from 1 to N?
ARecursion
BIteration
CBoth are equally efficient
DNeither works
✗ Incorrect
Iteration is simpler and more efficient for straightforward repeated tasks like summing numbers.
What must a recursive function always have to avoid infinite calls?
AA global variable
BA loop
CAn iteration counter
DA base case
✗ Incorrect
A base case stops recursion by providing a simple answer without further calls.
If a problem can be solved both ways, what is a reason to choose iteration?
ATo use more CPU
BTo make code longer
CTo reduce memory use and avoid stack overflow
DTo make debugging harder
✗ Incorrect
Iteration uses less memory and avoids the risk of stack overflow from deep recursion.
Explain in your own words when recursion is a better choice than iteration.
Think about problems like exploring branches of a tree.
You got /3 concepts.
Describe the main risks or downsides of using recursion without care.
What happens if the function keeps calling itself forever?
You got /4 concepts.
Practice
(1/5)
1. Which scenario is best suited for using recursion instead of iteration?
easy
A. When the problem requires simple repeated loops
B. When you want to avoid function call overhead
C. When memory usage must be minimal
D. When the problem can be divided into similar smaller problems
Solution
Step 1: Understand recursion use case
Recursion works best when a problem can be broken down into smaller similar problems, like tree traversals or factorial calculation.
Step 2: Compare with iteration scenarios
Iteration is better for simple repeated tasks without the need for breaking down problems.
Final Answer:
When the problem can be divided into similar smaller problems -> Option D
Quick Check:
Recursion = divide and conquer [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
Thinking iteration is always faster
Ignoring function call overhead in recursion
Confusing recursion with simple loops
2. Which of the following is the correct syntax for a recursive function in Python?
easy
A. def func(): return func()
B. def func(n): if n == 0: return 1 else: return n * func(n-1)
C. def func(n): while n > 0: n -= 1 return n
D. def func(n): for i in range(n): return i
Solution
Step 1: Identify recursive function structure
A recursive function calls itself with a smaller input and has a base case to stop recursion.
Step 2: Check each option
def func(n): if n == 0: return 1 else: return n * func(n-1) correctly defines a base case (n == 0) and recursive call (func(n-1)). Others either lack base case or use iteration.
Final Answer:
def func(n): if n == 0: return 1 else: return n * func(n-1) -> Option B
Quick Check:
Base case + self call = recursion [OK]
Hint: Look for base case and self call in function [OK]
Common Mistakes:
Missing base case causing infinite recursion
Using loops instead of recursive calls
Incorrect indentation or syntax errors
3. What is the output of the following Python code?
def factorial_iter(n):
result = 1
for i in range(1, n+1):
result *= i
return result
print(factorial_iter(4))