Recall & Review
beginner Click to reveal answer
What is the base case in recursion?
The base case is the condition that stops the recursion. It prevents infinite calls by returning a simple, direct answer.
beginner Click to reveal answer
What is the recursive case in recursion?
The recursive case is where the function calls itself with a smaller or simpler input, moving toward the base case.
intermediate Click to reveal answer
Why do we need both base case and recursive case in a recursive function?
The base case stops the recursion to avoid infinite loops, and the recursive case breaks the problem into smaller parts to solve it step by step.
beginner Click to reveal answer
Look at this Go code snippet:
What is the base case here?
func factorial(n int) int {
if n == 0 {
return 1
}
return n * factorial(n-1)
}What is the base case here?
The base case is
if n == 0 { return 1 }. It stops recursion when n reaches 0.beginner Click to reveal answer
In the same factorial function, what is the recursive case?
The recursive case is
return n * factorial(n-1). It calls the function itself with a smaller number.What happens if a recursive function has no base case?
✗ Incorrect
Without a base case, the function keeps calling itself endlessly, causing a stack overflow error.
In recursion, the base case is used to:
✗ Incorrect
The base case stops the recursion by providing a simple answer without further calls.
Which part of recursion breaks the problem into smaller parts?
✗ Incorrect
The recursive case calls the function itself with smaller inputs to solve the problem step by step.
In the factorial example, what is the result of factorial(0)?
✗ Incorrect
The base case returns 1 when n is 0, so factorial(0) is 1.
What is the main risk if the recursive case does not move toward the base case?
✗ Incorrect
If the recursive case does not reduce the problem size, the base case is never reached, causing infinite recursion.
Explain in your own words what the base case and recursive case are in recursion.
Think about how recursion stops and how it continues.
You got /3 concepts.
Describe how the factorial function uses base case and recursive case to calculate the result.
Focus on what happens when n is zero and when n is greater than zero.
You got /3 concepts.
