What is recursion in the context of arrays and strings?
Recursion is a way to solve problems by having a function call itself with smaller parts of the array or string until it reaches a simple case it can solve directly.
Click to reveal answer
beginner
How do you identify the base case in a recursive function for arrays or strings?
The base case is the simplest situation where the function can return an answer without calling itself again, often when the array or string is empty or has one element.
Click to reveal answer
beginner
Explain what happens during the 'recursive call' step.
During the recursive call, the function calls itself with a smaller part of the array or string, moving closer to the base case each time.
Click to reveal answer
beginner
Why is it important to reduce the problem size in each recursive call?
Reducing the problem size ensures the recursion moves toward the base case and eventually stops, preventing infinite loops.
Click to reveal answer
beginner
What is a common mistake when writing recursive functions on arrays or strings?
A common mistake is forgetting to define a proper base case or not reducing the problem size, which causes the function to call itself forever.
Click to reveal answer
What should a recursive function on a string do first?
APrint the whole string
BReverse the string immediately
CCheck if the string is empty or has one character
DCall itself without any change
✗ Incorrect
The function should first check the base case, which is usually when the string is empty or has one character.
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
CSorts the array
DPrints the array
✗ Incorrect
After handling the first element, the function calls itself with the remaining part of the array to continue processing.
What happens if a recursive function has no base case?
AIt runs forever and causes an error
BIt returns immediately
CIt works faster
DIt skips recursion
✗ Incorrect
Without a base case, the function keeps calling itself endlessly, leading to a stack overflow error.
Which of these is a valid base case for recursion on an array?
AArray is sorted
BArray length is 10
CArray contains only numbers
DArray length is 0
✗ Incorrect
The base case often checks if the array is empty (length 0) to stop recursion.
What is the main goal of recursion on strings or arrays?
ASort the data
BBreak the problem into smaller parts and solve each
CLoop through the data multiple times
DPrint the data
✗ Incorrect
Recursion breaks the problem into smaller pieces and solves each piece until the whole problem is solved.
Describe the steps to write a recursive function that reverses a string.
Think about how to reduce the string size and build the reversed string step by step.
You got /4 concepts.
Explain why recursion is useful for processing arrays and strings.
Consider how recursion handles parts of data one at a time.
You got /4 concepts.
Practice
(1/5)
1. What does a recursive function on an array typically do to process the array elements?
easy
A. It uses loops instead of calling itself.
B. It processes one element and calls itself on the rest of the array.
C. It processes all elements at once without calling itself.
D. It only processes the first and last elements.
Solution
Step 1: Understand recursion on arrays
Recursive functions handle one element and then call themselves on the smaller part (rest of the array).
Step 2: Identify the typical recursive pattern
This pattern reduces the problem size by one element each call until the array is empty.
Final Answer:
It processes one element and calls itself on the rest of the array. -> Option B
Quick Check:
Recursion = process one + recurse rest [OK]
Hint: Recursion breaks problem into smaller parts [OK]
Common Mistakes:
Thinking recursion processes all elements at once
Confusing recursion with loops
Ignoring the base case
2. Which of the following is the correct syntax to define a recursive function in JavaScript that takes an array as input?
easy
A. function recurse(arr) { if(arr.length === 0) return; recurse(arr.slice(1)); }
B. function recurse(arr) { while(arr.length > 0) recurse(arr); }
C. function recurse(arr) { for(let i=0; i
D. function recurse(arr) { if(arr.length === 0) return; recurse(arr.pop()); }
Solution
Step 1: Check base case and recursive call
function recurse(arr) { if(arr.length === 0) return; recurse(arr.slice(1)); } correctly checks if array is empty and calls itself with the rest of the array using slice(1).
Step 2: Identify incorrect syntax in other options
The version using while(arr.length > 0) recurse(arr); causes infinite recursion because it passes the same array; the version using for loop calls recurse on individual elements; the version using arr.pop() passes a single element, not an array.
Final Answer:
function recurse(arr) { if(arr.length === 0) return; recurse(arr.slice(1)); } -> Option A
Quick Check:
Base case + recurse smaller array = correct syntax [OK]
Hint: Base case + smaller input slice needed [OK]
Common Mistakes:
Missing base case causing infinite recursion
Passing wrong argument type in recursive call
Using loops inside recursion incorrectly
3. What will be the output of the following code?
function sumArray(arr) {
if (arr.length === 0) return 0;
return arr[0] + sumArray(arr.slice(1));
}
console.log(sumArray([1, 2, 3, 4]));
medium
A. 1234
B. Error
C. undefined
D. 10
Solution
Step 1: Understand the recursive sum logic
The function adds the first element to the sum of the rest recursively until array is empty.