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, like processing one element of an array or one character of a string at a time until a base case is reached.
Click to reveal answer
beginner
What is a base case in recursion?
A base case is the simplest situation in a recursive function that can be answered directly without further recursion, stopping the recursive calls.
Click to reveal answer
intermediate
How does recursion help in reversing a string?
Recursion reverses a string by taking the first character, calling itself on the rest of the string, and then adding the first character at the end of the reversed substring.
Click to reveal answer
intermediate
Explain how recursion can be used to find the sum of elements in an array.
Recursion sums array elements by adding the first element to the sum of the rest of the array, which is found by calling the same function on the smaller array until the base case (empty array) is reached.
Click to reveal answer
beginner
What happens if a recursive function does not have a proper base case?
Without a proper base case, the recursive function will keep calling itself forever, causing a stack overflow error and crashing the program.
Click to reveal answer
What is the first step in a recursive function processing an array?
AProcess the first element and call recursion on the rest
BProcess the last element only
CStop immediately without processing
DProcess all elements at once
✗ Incorrect
In recursion on arrays, we usually process the first element and then call the function recursively on the remaining elements.
What is the base case when recursively reversing a string?
AWhen the string has two characters
BWhen the string is empty or has one character
CWhen the string is longer than 5 characters
DWhen the string contains only vowels
✗ Incorrect
The base case for reversing a string recursively is when the string is empty or has one character, as it is already reversed.
Which of these is NOT a correct base case for recursion on arrays?
AArray length is greater than zero
BArray length is one
CArray length is negative
DArray length is zero
✗ Incorrect
Base cases are for smallest or simplest inputs, so array length greater than zero is not a base case.
What will happen if a recursive function lacks a base case?
AIt will return immediately
BIt will skip recursion
CIt will run forever until a crash
DIt will optimize itself
✗ Incorrect
Without a base case, recursion never stops and causes a stack overflow error.
How does recursion simplify problems on strings and arrays?
ABy sorting the data first
BBy using loops instead
CBy ignoring parts of the data
DBy breaking the problem into smaller similar problems
✗ Incorrect
Recursion works by dividing a problem into smaller versions of itself, making it easier to solve step by step.
Describe how you would use recursion to find the maximum element in an array.
Think about comparing one element at a time and reducing the problem size.
You got /3 concepts.
Explain the role of the base case and recursive case in a function that reverses a string recursively.
Focus on when recursion stops and how the string is rebuilt.
You got /3 concepts.
Practice
(1/5)
1. What is the main idea behind using recursion on arrays or strings?
easy
A. Sorting the array or string before processing
B. Using loops to iterate over each element
C. Breaking the problem into smaller similar problems until a base case is reached
D. Storing all elements in a new data structure
Solution
Step 1: Understand recursion concept
Recursion solves problems by calling itself with smaller inputs until a simple base case is reached.
Step 2: Apply to arrays or strings
For arrays or strings, recursion processes one element and then calls itself on the rest.
Final Answer:
Breaking the problem into smaller similar problems until a base case is reached -> Option C
Quick Check:
Recursion = divide and conquer [OK]
Hint: Think: solve small part, then repeat on smaller input [OK]
Common Mistakes:
Confusing recursion with loops
Ignoring the base case
Trying to process all elements at once
2. Which of the following is the correct base case for a recursive function that processes a string character by character?
easy
A. if (str[0] == '\0') return;
B. if (str == null) return;
C. if (str.Length > 0) return;
D. if (str.Length == 0) return;
Solution
Step 1: Identify base case for string recursion
The base case should stop recursion when the string is empty (length zero).
Step 2: Check options
if (str.Length == 0) return; correctly checks if string length is zero to stop recursion.
Final Answer:
if (str.Length == 0) return; -> Option D
Quick Check:
Base case = empty string check [OK]
Hint: Base case usually checks for empty string or array length zero [OK]
Common Mistakes:
Checking for null instead of empty string
Using wrong condition like length > 0
Checking for null character '\0' in C# strings
3. What will be the output of this C# recursive function call?
static int SumArray(int[] arr, int index) {
if (index == arr.Length) return 0;
return arr[index] + SumArray(arr, index + 1);
}
int[] nums = {1, 2, 3};
Console.WriteLine(SumArray(nums, 0));
medium
A. 6
B. 0
C. 3
D. StackOverflowException
Solution
Step 1: Trace recursive calls
SumArray called with index 0 returns arr[0] + SumArray(1) = 1 + ...
SumArray(1) returns 2 + SumArray(2)
SumArray(2) returns 3 + SumArray(3)
SumArray(3) returns 0 (base case)
Step 2: Calculate total sum
Sum = 1 + 2 + 3 + 0 = 6
Final Answer:
6 -> Option A
Quick Check:
Sum of [1,2,3] = 6 [OK]
Hint: Add current element + recursive sum of rest [OK]
Common Mistakes:
Forgetting base case leads to infinite recursion
Returning wrong index or value
Confusing index with length
4. Identify the error in this recursive function that reverses a string: