What is recursion in the context of arrays and strings?
Recursion is a method where a function calls itself to solve smaller parts of a problem, such as processing elements of an array or characters in a string one by one until a base case is reached.
Click to reveal answer
beginner
What is the base case in a recursive function working on an array?
The base case is the condition that stops the recursion, often when the index reaches the length of the array or when the array is empty, preventing infinite calls.
Click to reveal answer
intermediate
How does recursion help in reversing a string?
Recursion reverses a string by taking the first character, recursively reversing the rest of the string, and then appending the first character at the end, building the reversed string step-by-step.
Click to reveal answer
intermediate
Explain how to find the maximum element in an array using recursion.
To find the maximum element recursively, compare the current element with the maximum of the rest of the array found by a recursive call, returning the larger value until the entire array is processed.
Click to reveal answer
beginner
What happens if a recursive function on arrays or strings does not have a proper base case?
Without a proper base case, the recursive function will call itself indefinitely, causing a stack overflow error and crashing the program.
Click to reveal answer
What is the base case for a recursive function that processes an array from start to end?
AWhen the array has one element
BWhen the array is empty
CWhen the index is equal to the array length
DWhen the index is zero
✗ Incorrect
The base case is usually when the index reaches the array length, meaning all elements have been processed.
In recursion on strings, what does the function typically do with the first character?
AProcesses it after the recursive call on the rest
BIgnores it and processes the rest
CProcesses it before the recursive call on the rest
DDuplicates it
✗ Incorrect
The function usually processes the first character after recursively processing the rest of the string.
Which of these is NOT a good base case for recursion on arrays?
AIndex equals array length
BArray is empty
CIndex is negative
DIndex is zero
✗ Incorrect
Index zero is usually the start, not a stopping condition for recursion.
What is the main risk of missing a base case in recursion?
AStack overflow error
BInfinite loop in iteration
CSyntax error
DCompilation failure
✗ Incorrect
Without a base case, recursion never stops, causing a stack overflow error.
How does recursion simplify problems on arrays and strings?
ABy using loops instead of function calls
BBy breaking the problem into smaller similar problems
CBy avoiding any repetition
DBy sorting the data first
✗ Incorrect
Recursion breaks problems into smaller, similar problems, making them easier to solve step-by-step.
Describe how you would use recursion to reverse a string step-by-step.
Think about how the first character moves to the end after recursion.
You got /3 concepts.
Explain the importance of the base case in recursive functions working on arrays.
Without it, the function never stops calling itself.
You got /3 concepts.
Practice
(1/5)
1. What does a recursive function on an array typically do to process the elements?
easy
A. It calls itself with a smaller part of the array until a base case is reached
B. It uses loops to iterate over the array elements
C. It modifies the array without calling itself
D. It sorts the array before processing
Solution
Step 1: Understand recursion on arrays
Recursive functions reduce the problem size by calling themselves with smaller parts of the array.
Step 2: Identify base case and recursive call
The function stops when the array part is empty or has one element (base case), otherwise it calls itself with a smaller array.
Final Answer:
It calls itself with a smaller part of the array until a base case is reached -> Option A
Quick Check:
Recursion = smaller problem calls [OK]
Hint: Recursion breaks problem into smaller parts until simple case [OK]
Common Mistakes:
Thinking recursion uses loops instead of self-calls
Ignoring the base case in recursion
Assuming recursion sorts the array automatically
2. Which of the following is the correct syntax to define a recursive method in Java that takes an integer array and an index as parameters?
easy
A. void recurse(int arr[], index int) { ... }
B. public void recurse(int[] arr, int index) { ... }
C. public int recurse(int arr, int index) { ... }
D. public void recurse(int[] arr; int index) { ... }
Solution
Step 1: Check Java method syntax
Java methods require return type, method name, and parameters with types and commas.
Step 2: Validate parameter types and order
public void recurse(int[] arr, int index) { ... } correctly uses 'int[] arr' and 'int index' separated by a comma and proper method signature.
Final Answer:
public void recurse(int[] arr, int index) { ... } -> Option B
Hint: Remember Java method parameters need types and commas [OK]
Common Mistakes:
Using wrong parameter order or missing commas
Incorrect array parameter syntax
Missing return type in method definition
3. What will be the output of the following Java code?
public class Test {
public static void printArray(int[] arr, int index) {
if (index == arr.length) return;
System.out.print(arr[index] + " ");
printArray(arr, index + 1);
}
public static void main(String[] args) {
int[] nums = {1, 2, 3};
printArray(nums, 0);
}
}
medium
A. Compilation error
B. 3 2 1
C. 1 2 3 0
D. 1 2 3
Solution
Step 1: Trace the recursive calls
printArray prints arr[index] then calls itself with index+1 until index equals arr.length (3).
Step 2: Output elements in order
It prints 1, then 2, then 3, then stops without extra output.
Final Answer:
1 2 3 -> Option D
Quick Check:
Recursion prints array elements in order [OK]
Hint: Recursion prints then calls next index until end [OK]
Common Mistakes:
Expecting reverse order output
Thinking extra elements print after recursion ends
Confusing base case condition
4. Identify the error in this recursive method that tries to sum elements of an integer array:
public int sumArray(int[] arr, int index) {
if (index > arr.length) return 0;
return arr[index] + sumArray(arr, index + 1);
}
medium
A. Array parameter should be List<Integer>
B. Method should return void instead of int
C. Base case condition should be index >= arr.length
D. Recursive call should use index - 1
Solution
Step 1: Check base case condition
Index should stop recursion when it reaches arr.length, so condition must be index >= arr.length.
Step 2: Understand array bounds
Using index > arr.length allows index == arr.length, which causes ArrayIndexOutOfBoundsException.
Final Answer:
Base case condition should be index >= arr.length -> Option C
Quick Check:
Base case must prevent out-of-bounds access [OK]
Hint: Base case index must not exceed array length [OK]
Common Mistakes:
Using wrong base case condition causing runtime error
Changing return type incorrectly
Incorrect recursive index update
5. Given a string, write a recursive method to check if it is a palindrome (reads the same forwards and backwards). Which approach correctly describes the recursion?
hard
A. Compare first and last characters, then recurse on substring excluding them
B. Reverse the string using recursion and compare with original
C. Count characters recursively and check if count is even
D. Use recursion to sort characters and check equality
Solution
Step 1: Understand palindrome check logic
Check if first and last characters match, then check the substring inside recursively.
Step 2: Identify correct recursive approach
Compare first and last characters, then recurse on substring excluding them matches this logic, reducing problem size each call until base case (empty or one char string).
Final Answer:
Compare first and last characters, then recurse on substring excluding them -> Option A