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 checking each element of an array or each character of a string step-by-step until it reaches the end.
Click to reveal answer
beginner
How does a base case work in recursion on arrays or strings?
The base case stops the recursion. For arrays or strings, it usually means when the index reaches the length (end) or when the string is empty, so the function stops calling itself.
Click to reveal answer
intermediate
Explain how to reverse a string using recursion.
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. Repeat until the string is empty.
Click to reveal answer
beginner
What is the role of the recursive call in processing arrays?
The recursive call processes the smaller part of the array, usually by moving the index forward by one, so each call handles one element until the whole array is processed.
Click to reveal answer
intermediate
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 and stack overflow errors.
Click to reveal answer
What is the base case when recursively printing all elements of an array?
AWhen the first element is zero
BWhen the array is empty
CWhen the index is equal to the array length
DWhen the array has only one element
✗ Incorrect
The base case is reached when the index reaches the length of the array, meaning all elements have been processed.
In recursion on strings, what does the function usually do after processing the first character?
ACalls itself with the rest of the string
BReturns immediately
CProcesses the last character next
DSkips the next character
✗ Incorrect
The function calls itself with the remaining substring after the first character to process the string step-by-step.
Which of these is NOT a good base case for recursion on arrays?
AIndex is zero
BArray is empty
CIndex is negative
DIndex equals array length
✗ Incorrect
Index zero is usually the start, not a stopping condition, so it is not a good base case.
What happens if a recursive function on arrays does not reduce the index?
AIt will reverse the array
BIt will finish quickly
CIt will skip elements
DIt will run forever or crash
✗ Incorrect
Without reducing the index, the base case is never reached, causing infinite recursion and possible crash.
How can recursion help in checking if a string is a palindrome?
ABy sorting the string
BBy comparing first and last characters and calling itself on the middle substring
CBy counting characters only once
DBy reversing the string iteratively
✗ Incorrect
Recursion compares the first and last characters and then checks the substring inside recursively.
Describe how recursion works on an array to print all its elements.
Think about stopping condition and moving forward in the array.
You got /3 concepts.
Explain the steps to reverse a string using recursion.
Focus on how the string is broken down and rebuilt.
You got /3 concepts.
Practice
(1/5)
1. What does a recursive function do when processing an array?
easy
A. It only processes the first element and stops
B. It loops through the array without calling itself
C. It creates a new array each time without 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
A recursive function processes an array by calling itself with a smaller portion, usually by reducing the size or moving the index.
Step 2: Identify the base case
The recursion stops when the array is empty or the index reaches the end, preventing infinite calls.
Final Answer:
It calls itself with a smaller part of the array until a base case is reached -> Option D
Quick Check:
Recursion = smaller calls until base case [OK]
Hint: Recursion breaks problem into smaller parts until simple case [OK]
Common Mistakes:
Thinking recursion loops without reducing problem size
Ignoring the base case leading to infinite recursion
Confusing recursion with iteration
2. Which of the following is the correct syntax to define a recursive function in Go that processes a string?
easy
A. func recurse(s string) string { if len(s) == 0 { return "" } return string(s[0]) + recurse(s[1:]) }
B. func recurse(s string) string { for i := 0; i < len(s); i++ { return s[i] } }
C. func recurse(s string) { if len(s) == 0 { return } else { recurse(s) } }
D. func recurse(s string) string { return s + recurse(s) }
Solution
Step 1: Check function signature and base case
func recurse(s string) string { if len(s) == 0 { return "" } return string(s[0]) + recurse(s[1:]) } defines a function with a base case checking if string length is zero, returning empty string.
Step 2: Confirm recursive call reduces problem size
func recurse(s string) string { if len(s) == 0 { return "" } return string(s[0]) + recurse(s[1:]) } calls itself with substring s[1:], reducing string size each call, correctly building result.