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 a base case in recursion?
A base case is the simplest situation in recursion where the function stops calling itself to avoid infinite loops.
Click to reveal answer
intermediate
How does the call stack work with recursion?
Each recursive call adds a new layer to the call stack. When a base case is reached, the stack starts to remove layers as functions finish and return results.
Click to reveal answer
intermediate
What happens if a recursive function has no base case?
Without a base case, the function keeps calling itself forever, causing a stack overflow error because the call stack grows too large.
Click to reveal answer
beginner
Explain the flow of a simple factorial function using recursion.
The factorial function calls itself with smaller numbers until it reaches 0 (base case). Then it multiplies the numbers as the calls return, building the final result.
Click to reveal answer
What is the purpose of the base case in a recursive function?
ATo stop the recursion and prevent infinite calls
BTo call the function again
CTo increase the call stack size
DTo make the function run faster
✗ Incorrect
The base case stops recursion by providing a simple condition where the function returns without calling itself again.
What happens to the call stack when a recursive function calls itself?
AThe stack is cleared
BThe stack size decreases
CA new frame is added to the stack for each call
DNothing changes
✗ Incorrect
Each recursive call adds a new frame to the call stack, storing the function's state until it returns.
Which of these is a correct base case for a factorial function?
Afactorial(n) = n + factorial(n - 1)
Bfactorial(1) = 0
Cfactorial(n) = n * factorial(n + 1)
Dfactorial(0) = 1
✗ Incorrect
The factorial of 0 is defined as 1, which is the base case stopping recursion.
What error occurs if a recursive function never reaches a base case?
ASyntax error
BStack overflow error
CType error
DMemory leak
✗ Incorrect
Without a base case, recursion never stops, causing the call stack to overflow.
In recursion, what does 'unwinding the call stack' mean?
AReturning from recursive calls one by one
BAdding new calls to the stack
CClearing all variables
DStarting recursion again
✗ Incorrect
Unwinding means the function returns from each recursive call, removing frames from the call stack.
Describe how recursion uses the call stack to solve problems step-by-step.
Think about how each call waits for the next before finishing.
You got /4 concepts.
Explain why a base case is essential in recursion and what happens if it is missing.
Consider what would happen if the function never stops calling itself.
You got /4 concepts.
Practice
(1/5)
1. What happens when a recursive function calls itself without a stopping condition?
easy
A. It runs only once and stops
B. It causes an infinite loop leading to a stack overflow error
C. It immediately returns without doing anything
D. It throws a syntax error
Solution
Step 1: Understand recursion stopping condition
A recursive function must have a base case to stop calling itself; otherwise, it keeps calling endlessly.
Step 2: Effect of missing stopping condition
Without a stopping condition, the function calls stack up until memory runs out, causing a stack overflow error.
Final Answer:
It causes an infinite loop leading to a stack overflow error -> Option B
Quick Check:
Missing base case = infinite recursion [OK]
Hint: Always check for a base case to stop recursion [OK]
Common Mistakes:
Assuming recursion stops automatically
Confusing infinite loop with syntax error
Thinking recursion runs only once without base case
2. Which of the following is the correct syntax to define a recursive function in TypeScript?
easy
A. function recurse(n: number): number { if (n <= 0) return 0; return n + recurse(n - 1); }
B. function recurse(n: number) { while(n > 0) recurse(n - 1); }
C. function recurse(n: number): number { return n + recurse(n + 1); }
D. function recurse(n: number): number { return n * recurse(n); }
Solution
Step 1: Check for base case and recursive call
function recurse(n: number): number { if (n <= 0) return 0; return n + recurse(n - 1); } has a base case (n <= 0) and calls itself with n - 1, which is correct for recursion.
Step 2: Analyze other options for errors
The version with a while loop lacks a proper base case and return statement; another calls recurse with n + 1 causing infinite recursion; another calls recurse with the same n causing infinite recursion.
Final Answer:
function recurse(n: number): number { if (n <= 0) return 0; return n + recurse(n - 1); } -> Option A
Quick Check:
Base case + decreasing argument = correct recursion [OK]
Hint: Look for base case and argument reducing in recursion [OK]
Common Mistakes:
Missing base case
Increasing argument causing infinite recursion
Using loops instead of recursion
3. What is the output of the following TypeScript code?
function factorial(n: number): number {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(3));