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 programming?
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 function calls, especially for simple loops or when the problem does not break down naturally.
Click to reveal answer
beginner
What is a risk of using recursion too deeply?
Using recursion too deeply can cause a stack overflow error because each function call uses memory on the call stack.
Click to reveal answer
Which of these problems is best solved using recursion?
ACalculating factorial of a number
BSumming numbers from 1 to 100
CPrinting numbers from 1 to 10
DFinding the maximum in a list using a loop
✗ Incorrect
Factorial calculation naturally breaks down into smaller factorial problems, making recursion a good fit.
What is a main disadvantage of recursion compared to iteration?
AAlways slower than iteration
BCannot solve complex problems
CUses more memory due to function call stack
DDoes not work with loops
✗ Incorrect
Recursion uses extra memory for each function call on the call stack, which iteration avoids.
Which method is usually more efficient in terms of memory?
ARecursion
BDepends on the programming language
CBoth use the same memory
DIteration
✗ Incorrect
Iteration uses constant memory for loops, while recursion uses extra memory for each call.
Which of these is a sign that recursion is a good choice?
AProblem can be divided into smaller similar problems
BProblem requires simple repeated steps
CProblem needs to run very fast
DProblem has no base case
✗ Incorrect
Recursion works well when the problem breaks down into smaller similar problems.
What happens if a recursive function has no base case?
AIt throws a syntax error
BIt runs forever and causes stack overflow
CIt converts to iteration
DIt automatically stops after some calls
✗ Incorrect
Without a base case, recursion never stops and causes a stack overflow error.
Explain when you should choose recursion over iteration and why.
Think about problems that repeat the same pattern on smaller parts.
You got /4 concepts.
Describe the main risks or downsides of using recursion.
Consider what happens inside the computer when functions call themselves.
You got /4 concepts.
Practice
(1/5)
1. Which scenario is best suited for using recursion instead of iteration?
easy
A. When the problem can be divided into similar smaller subproblems
B. When you need to process a large list without stack overflow risk
C. When you want to avoid function call overhead
D. When the problem requires simple repeated loops
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 A
Quick Check:
Recursion fits divide-and-conquer problems [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
Thinking iteration is always better
Ignoring stack overflow risk in recursion
Confusing recursion with simple loops
2. Which of the following is the correct syntax to write a recursive function in C++?
easy
A. void func() { if(condition) func(); }
B. void func() { func(); }
C. void func() { while(true) func(); }
D. void func() { for(int i=0; i<10; i++) func(); }
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 A
Quick Check:
Recursion needs a stopping condition [OK]
Hint: Recursive functions must have a stopping condition [OK]
Common Mistakes:
Writing recursion without base case
Using loops inside recursion incorrectly
Confusing infinite loops with recursion
3. What is the output of the following C++ code?
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
std::cout << factorial(4);
return 0;
}