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 factorial calculation, making the code simpler and easier to understand.
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 the problem can be solved with repeated steps.
Click to reveal answer
intermediate
What is a risk of using recursion too much?
Using recursion too much can cause a stack overflow error because each function call uses memory, and too many calls can fill up the call stack.
Click to reveal answer
Which method is usually easier to write for problems like tree traversal?
ANeither works
BIteration
CBoth are equally easy
DRecursion
✗ Incorrect
Recursion naturally fits tree traversal because it breaks the problem into smaller similar parts.
What is a common downside of recursion compared to iteration?
ACannot solve complex problems
BRuns slower because loops are slower
CUses more memory due to function calls
DAlways harder to understand
✗ Incorrect
Recursion uses more memory because each call adds to the call stack.
Which approach is better for simple repeated steps like counting from 1 to 10?
AIteration
BBoth are equally good
CRecursion
DNeither works
✗ Incorrect
Iteration is simpler and more efficient for simple repeated steps.
What happens if recursion goes too deep without a base case?
AProgram runs faster
BStack overflow error occurs
CProgram ignores extra calls
DRecursion automatically stops
✗ Incorrect
Without a base case, recursion keeps calling itself until the call stack is full, causing an error.
Which is true about iteration?
AIt uses loops to repeat instructions
BIt always uses less memory than recursion
CIt cannot solve problems with repeated steps
DIt is slower than recursion
✗ Incorrect
Iteration uses loops like for or while to repeat instructions.
Explain when you would choose recursion over iteration and why.
Think about problems that naturally repeat themselves in smaller chunks.
You got /3 concepts.
Describe the risks of using recursion and how iteration can help avoid them.
Consider what happens when too many function calls happen.
You got /3 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 the problem can be divided into smaller similar subproblems
C. When you want to avoid function call overhead
D. When memory usage must be minimal
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 smaller similar subproblems -> Option B
Quick Check:
Recursion fits divide-and-conquer = C [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 simple loops with recursive problems
2. Which of the following is the correct syntax for a recursive function in JavaScript?
easy
A. function recurse(n) { while(n > 0) { n--; } return n; }
B. function recurse(n) { for(let i=0; i
C. function recurse(n) { if (n <= 0) return 0; return n + recurse(n - 1); }
D. function recurse(n) { if(n > 0) { return n * recurse(n + 1); } else { return 1; } }
Solution
Step 1: Identify recursive call and base case
function recurse(n) { if (n <= 0) return 0; return n + recurse(n - 1); } has a base case (n <= 0) and calls itself with a smaller argument (n - 1), which is correct recursion.
Step 2: Check other options for recursion errors
function recurse(n) { while(n > 0) { n--; } return n; } uses a loop, not recursion. function recurse(n) { for(let i=0; i 0) { return n * recurse(n + 1); } else { return 1; } } calls recurse with n + 1, causing infinite recursion.
Final Answer:
function recurse(n) { if (n <= 0) return 0; return n + recurse(n - 1); } -> Option C
Quick Check:
Base case + smaller recursive call = B [OK]
Hint: Look for base case and smaller recursive call [OK]
Common Mistakes:
Missing base case causing infinite recursion
Using loops instead of recursive calls
Increasing argument in recursive call causing stack overflow
3. What is the output of the following JavaScript code?
function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(4));