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
Why do we need recursion when we have loops?
Some problems, like exploring tree structures or solving puzzles, are easier and cleaner to solve with recursion because loops can't naturally handle nested or branching steps.
Click to reveal answer
intermediate
Give an example of a problem that loops cannot express cleanly but recursion can.
Walking through a family tree or exploring folders inside folders is hard with loops but easy with recursion because recursion naturally handles nested layers.
Click to reveal answer
beginner
What is a base case in recursion?
A base case is the simplest part of the problem that recursion can solve directly without calling itself again. It stops the recursion from going forever.
Click to reveal answer
intermediate
How does recursion help in expressing problems with branching paths?
Recursion can call itself multiple times for each branch, naturally exploring all paths one by one, which is hard to do cleanly with loops.
Click to reveal answer
What stops a recursive function from running forever?
AA variable that counts loops
BA loop inside the function
CA base case that ends recursion
DA timer that stops the function
✗ Incorrect
A base case is the condition where the recursive function stops calling itself, preventing infinite recursion.
Which problem is easier to solve with recursion than loops?
AFinding the largest number in a list
BExploring nested folders
CSumming numbers from 1 to 10
DPrinting numbers from 1 to 5
✗ Incorrect
Exploring nested folders involves branching and layers, which recursion handles naturally, unlike simple loops.
What does recursion do when it calls itself?
ARepeats the same step without change
BCreates a loop
CEnds the program
DSolves a smaller part of the problem
✗ Incorrect
Each recursive call works on a smaller or simpler part of the problem until it reaches the base case.
Why are loops not good for problems with branching paths?
ALoops only handle one path at a time
BLoops are slower than recursion
CLoops cannot repeat
DLoops use more memory
✗ Incorrect
Loops handle one sequence of steps, but branching paths require exploring multiple paths, which recursion can do by calling itself multiple times.
Which of these is a key part of writing a recursive function?
ADefining a base case
BUsing only loops
CAvoiding function calls
DUsing global variables
✗ Incorrect
Defining a base case is essential to stop recursion and avoid infinite calls.
Explain why recursion is useful for problems with nested or branching structures compared to loops.
Think about how folders inside folders or family trees are explored.
You got /4 concepts.
Describe what a base case is and why it is important in recursion.
Without it, recursion would never stop.
You got /4 concepts.
Practice
(1/5)
1. Why do we use recursion instead of loops in some problems?
easy
A. Because recursion always uses less memory than loops
B. Because loops are slower than recursion in all cases
C. Because recursion can express problems with self-similar structure more naturally
D. Because loops cannot perform repeated actions
Solution
Step 1: Understand recursion's purpose
Recursion is useful when a problem can be broken down into smaller versions of itself, like tree traversal or factorial calculation.
Step 2: Compare recursion and loops
Loops repeat actions but do not naturally express self-similar or nested structures, which recursion handles clearly.
Final Answer:
Because recursion can express problems with self-similar structure more naturally -> Option C
Quick Check:
Recursion = natural for self-similar problems [OK]
Hint: Recursion fits problems with repeated smaller subproblems [OK]
Common Mistakes:
Thinking loops are always faster
Believing recursion always uses less memory
Assuming loops cannot repeat actions
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 1 } else { return n * factorial(n-1) } }
B. func factorial(n int) { if n == 0 { return 1 } else { 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 0 } else { return n * factorial(n-1) } }
Solution
Step 1: Check function return type and base case
func factorial(n int) int { if n == 0 { return 1 } else { return n * factorial(n-1) } } correctly defines a function returning int and has a base case returning 1 when n == 0.
Step 2: Verify recursive call and return statement
func factorial(n int) int { if n == 0 { return 1 } else { return n * factorial(n-1) } } correctly returns n multiplied by factorial(n-1), matching factorial definition.
Final Answer:
func factorial(n int) int { if n == 0 { return 1 } else { return n * factorial(n-1) } } -> Option A
Quick Check:
Correct recursive syntax = func factorial(n int) int { if n == 0 { return 1 } else { return n * factorial(n-1) } } [OK]
Hint: Look for base case and correct return type in recursion [OK]
Common Mistakes:
Missing return type in function signature
Returning wrong base case value
Using loops instead of recursion incorrectly
3. What is the output of this Go code?
func sum(n int) int {
if n == 0 {
return 0
}
return n + sum(n-1)
}
func main() {
fmt.Println(sum(3))
}
medium
A. Error: undefined fmt
B. 3
C. 0
D. 6
Solution
Step 1: Analyze the sum function logic
The sum function recursively adds numbers from n down to 0, so sum(3) = 3 + 2 + 1 + 0 = 6.
Step 2: Check code completeness and imports
The code uses fmt.Println but does not import "fmt", causing a compile error.