What is recursion in the context of arrays and strings?
Recursion is a way where a function calls itself to solve smaller parts of a problem, like breaking an array or string into smaller pieces until it reaches a simple case.
Click to reveal answer
beginner
How do you identify the base case in recursion on arrays or strings?
The base case is the simplest part where the function stops calling itself, often when the array or string is empty or has one element.
Click to reveal answer
intermediate
Explain how recursion can be used to reverse a string.
To reverse a string recursively, take the first character, call the function on the rest of the string, then add the first character at the end of the result.
Click to reveal answer
beginner
What happens if a recursive function on an array does not have a proper base case?
Without a proper base case, the function will keep calling itself forever, causing a stack overflow error.
Click to reveal answer
beginner
Show a simple recursive approach to find the sum of elements in an array.
Sum the first element and then call the function on the rest of the array until the array is empty (base case).
Click to reveal answer
What is the base case when recursively processing a string?
AWhen the string is longer than the array
BWhen the string has more than 10 characters
CWhen the string contains only vowels
DWhen the string is empty or has one character
✗ Incorrect
The base case is when the string is empty or has one character, which stops further recursion.
In recursion on arrays, what does the function usually do after processing the first element?
ACalls itself with the rest of the array
BReturns the first element immediately
CProcesses the last element next
DStops recursion
✗ Incorrect
After processing the first element, the function calls itself with the rest of the array to continue the process.
What error occurs if recursion has no base case?
ASyntax error
BStack overflow
CType error
DMemory leak
✗ Incorrect
Without a base case, recursion never stops, causing a stack overflow error.
Which of these is a good example of a base case for recursion on an array?
AArray is sorted
BArray length is greater than 5
CArray length is zero
DArray contains only even numbers
✗ Incorrect
The base case is usually when the array length is zero, meaning no more elements to process.
How can recursion help in reversing an array?
ABy swapping first and last elements and calling itself on the middle part
BBy sorting the array
CBy adding elements to the end
DBy removing duplicates
✗ Incorrect
Recursion can reverse an array by swapping the first and last elements and then calling itself on the smaller middle part.
Explain how recursion works on arrays with an example of summing all elements.
Think about how to break the array into smaller parts and when to stop.
You got /4 concepts.
Describe the steps to reverse a string using recursion.
Focus on how the function processes one character and calls itself on the rest.
You got /4 concepts.
Practice
(1/5)
1. What does a recursive function on an array typically do?
easy
A. It only works on strings, not arrays.
B. It loops through the array without calling itself.
C. It creates a new array without using recursion.
D. It calls itself with a smaller part of the array until a base case is reached.
Solution
Step 1: Understand recursion on arrays
Recursive functions on arrays call themselves with smaller parts of the array to solve the problem step-by-step.
Step 2: Identify the base case
The recursion stops when the array is empty or has one element, which is the base case.
Final Answer:
It calls itself with a smaller part of the array until a base case is reached. -> Option D
Quick Check:
Recursion = smaller parts + base case [OK]
Hint: Recursion breaks problem into smaller parts until simple case [OK]
Common Mistakes:
Thinking recursion loops without calling itself
Confusing recursion with iteration
Believing recursion only works on strings
2. Which of the following is the correct TypeScript syntax to define a recursive function that sums elements of a number array?
easy
A. function sum(arr: number[]): number { return arr[0] + sum(arr.slice(1)); }
B. function sum(arr: number[]): number { if(arr.length === 0) return 1; return arr[0] + sum(arr.slice(1)); }
C. function sum(arr: number[]): number { if(arr.length === 0) return 0; return arr[0] + sum(arr.slice(1)); }
D. function sum(arr: number[]): number { if(arr.length === 0) return 0; return sum(arr.slice(1)); }
Solution
Step 1: Check base case correctness
The base case should return 0 when array is empty to stop recursion correctly.
Step 2: Check recursive call and addition
The function adds the first element to the sum of the rest of the array recursively.
Final Answer:
function sum(arr: number[]): number { if(arr.length === 0) return 0; return arr[0] + sum(arr.slice(1)); } -> Option C
Quick Check:
Base case returns 0 + recursive sum [OK]
Hint: Base case returns 0; recursive call adds first element [OK]
Common Mistakes:
Missing base case causing infinite recursion
Returning wrong base case value like 1
Not adding the first element before recursive call
3. What is the output of this TypeScript code?
function reverseString(s: string): string {
if (s === "") return "";
return reverseString(s.slice(1)) + s[0];
}
console.log(reverseString("abc"));