What if you could solve huge problems by solving tiny pieces again and again, without getting stuck?
Why Recursion Base Case and Recursive Case in DSA Typescript?
Imagine you want to find the total number of steps to climb a tall staircase, but you try to count each step one by one manually without any plan.
It feels endless and confusing when the staircase is very tall.
Counting each step manually is slow and easy to lose track.
Without a clear stopping point, you might keep counting forever or forget where you started.
Recursion breaks the problem into smaller parts and uses a clear stopping point called the base case.
This way, the counting stops automatically when it reaches the bottom step, making the process neat and error-free.
function countSteps(steps: number): number {
let total = 0;
for (let i = 1; i <= steps; i++) {
total += 1;
}
return total;
}function countSteps(steps: number): number {
if (steps === 0) return 0; // base case
return 1 + countSteps(steps - 1); // recursive case
}Recursion with base and recursive cases lets you solve big problems by solving smaller versions of the same problem step-by-step.
When you open nested folders on your computer, recursion helps the system find files inside folders inside folders without getting lost.
Base case stops the recursion to avoid endless loops.
Recursive case breaks the problem into smaller parts.
Together, they make complex problems easier to solve step-by-step.
Practice
base case in a recursive function?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 BQuick Check:
Base case = stop recursion [OK]
- Thinking base case calls recursion again
- Confusing base case with recursive case
- Ignoring base case leads to infinite recursion
Solution
Step 1: Check base case correctness
function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } usesif (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); } callsfactorial(n - 1)and multiplies byn, which matches factorial definition.Final Answer:
function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } -> Option AQuick Check:
Correct base and recursive case syntax = function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } [OK]
- Using while loop instead of recursion
- Wrong base case value
- Recursive call increasing n causing infinite recursion
function sumTo(n: number): number {
if (n === 0) return 0;
return n + sumTo(n - 1);
}
console.log(sumTo(3));Solution
Step 1: Trace recursive calls
sumTo(3) calls 3 + sumTo(2), sumTo(2) calls 2 + sumTo(1), sumTo(1) calls 1 + sumTo(0), sumTo(0) returns 0.Step 2: Calculate return values
sumTo(1) = 1 + 0 = 1, sumTo(2) = 2 + 1 = 3, sumTo(3) = 3 + 3 = 6.Final Answer:
6 -> Option AQuick Check:
Sum 1+2+3 = 6 [OK]
- Confusing base case return value
- Adding instead of multiplying
- Missing base case causing infinite recursion
function countdown(n: number): void {
if (n === 0) return;
console.log(n);
countdown(n + 1);
}
countdown(3);Solution
Step 1: Analyze recursive call direction
The function callscountdown(n + 1), which increases n, causing infinite recursion.Step 2: Fix recursive call to decrease n
Changing tocountdown(n - 1)moves towards base casen === 0, stopping recursion.Final Answer:
Changecountdown(n + 1)tocountdown(n - 1)-> Option DQuick Check:
Recursive call must approach base case [OK]
- Increasing input causing infinite recursion
- Missing or wrong base case
- Changing print statement instead of recursion
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?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 CQuick Check:
Empty array stops recursion, nested arrays flatten recursively [OK]
- Wrong base case returning number instead of array
- Not checking element type before recursion
- Infinite recursion by missing base case
Ready to prove it?
Pass the test and this pattern moves unknown
