The base case is the condition that stops the recursion. It prevents infinite calls by returning a simple, direct answer.
Click to reveal answer
beginner
What is the recursive case in recursion?
The recursive case is where the function calls itself with a smaller or simpler input, moving towards the base case.
Click to reveal answer
intermediate
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.
Click to reveal answer
beginner
In the factorial function, what is the base case and what is the recursive case?<br><pre>function factorial(n: number): number { if (n === 0) return 1; return n * factorial(n - 1); }</pre>
Base case: when n === 0, return 1. Recursive case: return n * factorial(n - 1), calling itself with smaller n.
Click to reveal answer
beginner
What happens if a recursive function has no base case?
The function will keep calling itself forever, causing a stack overflow error because it never stops.
Click to reveal answer
What does the base case in recursion do?
ACreates a loop
BCalls the function again
CIncreases the input size
DStops the recursion
✗ Incorrect
The base case stops the recursion by providing a direct answer.
Which part of recursion calls the function itself?
ARecursive case
BBase case
CLoop case
DExit case
✗ Incorrect
The recursive case calls the function itself with a smaller input.
What will happen if a recursive function lacks a base case?
AIt will return zero
BIt will stop immediately
CIt will run forever causing an error
DIt will run once
✗ Incorrect
Without a base case, recursion never stops and causes a stack overflow error.
In the factorial function, what is the base case?
An === 0
Bn === 1
Cn > 0
Dn < 0
✗ Incorrect
The base case for factorial is when n equals 0, returning 1.
Why is the recursive case important?
AIt stops the recursion
BIt breaks the problem into smaller parts
CIt increases the problem size
DIt returns the final answer
✗ Incorrect
The recursive case breaks the problem into smaller parts to solve it step by step.
Explain the roles of the base case and recursive case in a recursive function.
Think about how recursion stops and how it continues.
You got /3 concepts.
Describe what happens when a recursive function is missing a base case.
What happens if the function never stops calling itself?
You got /3 concepts.
Practice
(1/5)
1. What is the purpose of the base case in a recursive function?
easy
A. To call the function again with a smaller input
B. To stop the recursion and prevent infinite calls
C. To increase the input size for the next call
D. To print the result of the recursion
Solution
Step 1: Understand what recursion does
Recursion calls the same function repeatedly with smaller inputs.
Step 2: Identify the role of the base case
The base case stops the recursion by providing a condition where the function returns without calling itself again.
Final Answer:
To stop the recursion and prevent infinite calls -> Option B
Quick Check:
Base case = stop recursion [OK]
Hint: Base case stops recursion to avoid infinite loops [OK]
Common Mistakes:
Thinking base case calls recursion again
Confusing base case with recursive case
Ignoring base case leads to infinite recursion
2. Which of the following is the correct syntax for a recursive function in TypeScript that calculates factorial?
easy
A. function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); }
B. function factorial(n: number): number { while (n > 0) return n * factorial(n - 1); }
C. function factorial(n: number): number { if (n === 1) return 0; else return n + factorial(n - 1); }
D. function factorial(n: number): number { return n * factorial(n + 1); }
Solution
Step 1: Check base case correctness
function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } uses if (n === 0) return 1; which is correct for factorial base case.
Step 2: Check recursive call correctness
function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } calls factorial(n - 1) and multiplies by n, which matches factorial definition.
Final Answer:
function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } -> Option A
Quick Check:
Correct base and recursive case syntax = function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } [OK]
Hint: Base case returns 1 for factorial, recursive calls with n-1 [OK]
Common Mistakes:
Using while loop instead of recursion
Wrong base case value
Recursive call increasing n causing infinite recursion
3. What will be the output of this TypeScript code?
function sumTo(n: number): number {
if (n === 0) return 0;
return n + sumTo(n - 1);
}
console.log(sumTo(3));
Hint: Add n plus sumTo(n-1) until base case 0 [OK]
Common Mistakes:
Confusing base case return value
Adding instead of multiplying
Missing base case causing infinite recursion
4. Identify the error in this recursive function and select the fix.
function countdown(n: number): void {
if (n === 0) return;
console.log(n);
countdown(n + 1);
}
countdown(3);
medium
A. Change console.log(n) to console.log(n - 1)
B. Add a base case for n === 1
C. Remove the base case to allow infinite recursion
D. Change countdown(n + 1) to countdown(n - 1)
Solution
Step 1: Analyze recursive call direction
The function calls countdown(n + 1), which increases n, causing infinite recursion.
Step 2: Fix recursive call to decrease n
Changing to countdown(n - 1) moves towards base case n === 0, stopping recursion.
Final Answer:
Change countdown(n + 1) to countdown(n - 1) -> Option D
Quick Check:
Recursive call must approach base case [OK]
Hint: Recursive calls must reduce input to reach base case [OK]
Common Mistakes:
Increasing input causing infinite recursion
Missing or wrong base case
Changing print statement instead of recursion
5. You want to write a recursive function flatten that takes a nested array of numbers (e.g., [1, [2, [3]], 4]) and returns a flat array [1, 2, 3, 4]. Which approach correctly uses base and recursive cases?
hard
A. Base case: always return input; Recursive case: never call flatten recursively
B. Base case: if input is number, return number; Recursive case: call flatten on input
C. Base case: if input is empty array, return empty array; Recursive case: for each element, if array, call flatten, else add element
D. Base case: if input is array, return input; Recursive case: if input is number, call flatten
Solution
Step 1: Define base case for empty array
If the input array is empty, return an empty array to stop recursion.
Step 2: Define recursive case for nested arrays
For each element, if it is an array, recursively call flatten; if not, add element to result.
Final Answer:
Base case: if input is empty array, return empty array; Recursive case: for each element, if array, call flatten, else add element -> Option C