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
When is iteration usually 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 doesn't naturally split into smaller parts.
Click to reveal answer
beginner
Why can recursion be easier to understand for some problems?
Recursion matches how we think about some problems naturally, like breaking a big task into smaller similar tasks, making the code simpler and clearer.
Click to reveal answer
beginner
What is a base case in recursion?
A base case is the simplest part of the problem that stops the recursion by giving a direct answer without calling itself again.
Click to reveal answer
intermediate
Give an example when recursion wins over iteration.
Recursion wins when solving problems like tree traversals or exploring all combinations, where the problem naturally splits into smaller similar parts.
Click to reveal answer
Which of these is a key advantage of iteration over recursion?
AUses less memory by avoiding many function calls
BEasier to write for problems like tree traversal
CAlways faster for all problems
DAutomatically handles complex branching
✗ Incorrect
Iteration uses a single loop and less memory, while recursion uses extra memory for each function call.
What must a recursive function always have to avoid infinite calls?
AAn iterator
BA loop
CA global variable
DA base case
✗ Incorrect
A base case stops the recursion by providing a simple answer.
When is recursion preferred over iteration?
AWhen the problem naturally breaks into smaller similar problems
BWhen you want to use less memory
CWhen the problem is a simple counting loop
DWhen performance is the only concern
✗ Incorrect
Recursion fits problems like tree traversal or divide-and-conquer where the problem splits naturally.
What is a downside of recursion compared to iteration?
ADoes not work with loops
BCannot solve complex problems
CUses more memory due to call stack
DAlways slower in all cases
✗ Incorrect
Each recursive call adds a new layer to the call stack, using more memory.
Which problem is best solved using recursion?
AAdding numbers from 1 to 100
BCalculating factorial of a number
CPrinting numbers from 1 to 10
DFinding the maximum in a list
✗ Incorrect
Factorial naturally fits recursion because it is defined in terms of itself.
Explain when you should choose recursion over iteration and why.
Think about problems that break down into smaller parts.
You got /4 concepts.
Describe the main disadvantages of recursion compared to iteration.
Consider what happens inside the computer when functions call themselves.
You got /4 concepts.
Practice
(1/5)
1. Which situation is best suited for using recursion instead of iteration?
easy
A. When the problem can be divided into similar smaller problems
B. When you want to avoid function calls
C. When you need to use less memory
D. When the problem has a fixed number of steps
Solution
Step 1: Understand recursion use case
Recursion works well when a problem can be broken down into smaller similar problems, like factorial or tree traversal.
Step 2: Compare with iteration
Iteration is better for fixed steps or simple loops, but recursion shines in divide-and-conquer problems.
Final Answer:
When the problem can be divided into similar smaller problems -> Option A
Quick Check:
Recursion = divide and conquer [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
Thinking recursion always uses less memory
Believing iteration is always faster
Confusing fixed steps with recursive breakdown
2. Which of the following is the correct syntax for a recursive function in Go?
easy
A. func factorial(n int) int { if n == 0 { return 0 } return n * factorial(n-1) }
B. func factorial(n int) { if n == 0 { return 1 } return n * factorial(n-1) }
C. func factorial(n int) int { for n > 0 { return n * factorial(n-1) } return 1 }
D. func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) }
Solution
Step 1: Check function signature and return type
func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } correctly defines factorial with int input and int return type.
Step 2: Verify base case and recursive call
func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } returns 1 when n is 0 and calls factorial(n-1) correctly.
Final Answer:
func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } -> Option D
Quick Check:
Correct recursion syntax = func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } [OK]
Hint: Look for correct return type and base case in recursion [OK]
Common Mistakes:
Missing return type in function signature
Incorrect base case return value
Using loop inside recursive function incorrectly
3. What is the output of the following Go code?
import "fmt"
func sum(n int) int {
if n == 0 {
return 0
}
return n + sum(n-1)
}
func main() {
fmt.Println(sum(3))
}
4. Identify the error in this iterative factorial function in Go and fix it:
func factorial(n int) int {
result := 1
for i := 1; i <= n; i++ {
result = result + i
}
return result
}
medium
A. Initialize result to 0 instead of 1
B. Change loop to decrement i
C. Change '+' to '*' in result update
D. Add base case for n == 0
Solution
Step 1: Analyze the loop operation
The loop adds i to result instead of multiplying, so it calculates sum not factorial.
Step 2: Fix the operation to multiplication
Change 'result = result + i' to 'result = result * i' to compute factorial correctly.
Final Answer:
Change '+' to '*' in result update -> Option C
Quick Check:
Factorial uses multiplication, not addition [OK]
Hint: Factorial multiplies all numbers, not adds [OK]
Common Mistakes:
Using addition instead of multiplication
Not initializing result to 1
Looping backwards unnecessarily
5. You want to process a large tree structure with unknown depth. Which approach is better and why?
Options: A) Use iteration with an explicit stack to avoid stack overflow. B) Use recursion for simplicity but risk stack overflow. C) Use recursion with tail call optimization to save memory. D) Use iteration with multiple nested loops for clarity.
hard
A. Use iteration with an explicit stack to avoid stack overflow.
B. Use recursion for simplicity but risk stack overflow.
C. Use recursion with tail call optimization to save memory.
D. Use iteration with multiple nested loops for clarity.
Solution
Step 1: Understand recursion limits on large unknown depth
Recursion can cause stack overflow if the tree is very deep because each call uses stack memory.
Step 2: Use iteration with explicit stack to control memory
Iteration with a manual stack simulates recursion but avoids system stack overflow, making it safer for large trees.
Final Answer:
Use iteration with an explicit stack to avoid stack overflow. -> Option A
Quick Check:
Iteration + explicit stack = safe deep tree traversal [OK]
Hint: Use iteration with stack to handle deep recursion safely [OK]
Common Mistakes:
Assuming recursion always safe for deep trees
Believing Go supports tail call optimization
Using nested loops for unknown depth structures
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.