Check palindrome using recursion only
Filed under MAQ Software
Free read 1 of 3An empty string or single-character string is inherently a palindrome; these serve as base cases.
The question
Given a string `s`, determine whether it is a palindrome using only recursion. A string is a palindrome if it reads the same backward as forward.
Constraints
- 0 <= s.length <= 10^4
- String contains printable ASCII characters.
Examples
- Input
- s = "racecar"
- Output
- true
- Why
- "racecar" reads identically in reverse.
- Input
- s = "hello"
- Output
- false
- Why
- "hello" reversed is "olleh", which is not equal.
Approach
An empty string or single-character string is inherently a palindrome; these serve as base cases.
Define a recursive helper that takes indices `left` and `right` to avoid substring allocations.
Check if `s[left] == s[right]`. If equal, recurse on `(left + 1, right - 1)`. Otherwise, return `False`.
Handle empty strings and strings of length two cleanly.
Solution
O(n) time, O(n) space
class Solution:
def isPalindrome(self, s: str) -> bool:
def helper(left: int, right: int) -> bool:
if left >= right:
return True
if s[left] != s[right]:
return False
return helper(left + 1, right - 1)
return helper(0, len(s) - 1)
Tips
- What they are testing: Tests base case design (len <= 1) and recursive boundary tracking.
- Common mistake: Creating string slices at each recursive step, causing unnecessary O(n^2) space complexity.
Related MAQ Software questions
Collected from public candidate posts and coding forums. Updated Nov 2026.