PPractick

DSA interview question

Check if a String is a Palindrome Using Recursion

Easy · Algorithms · Topics: recursion, string, two pointer

The problem

A word-game app highlights palindromes, words that read the same forwards and backwards, like racecar or abba. The check itself is easy with a loop, but the exercise is to express it recursively: a string is a palindrome if its first and last characters match and the part in between is also a palindrome. This problem shows recursion returning a boolean, where one mismatch anywhere makes the whole answer false and short-circuits the remaining calls.

Example: "racecar" → true, "abc" → false.

Function signature

bool isPalindrome(const string& s, int left, int right);  // call with (s, 0, s.size() - 1)

Test cases

Input sOutputExplanation
"abba"trueEven length
"racecar"trueOdd length; the middle character is its own mirror
"abc"false'a' ≠ 'c' at the first comparison
"a"trueA single character
""trueThe empty string reads the same both ways
"Racecar"falseCase-sensitive: 'R' ≠ 'r'
"abca"falseEnds match, but "bc" in the middle doesn't

(Confirm: comparison is exact and case-sensitive; a variant that ignores case and non-letters is discussed in the follow-ups.)

How interviewers phrase it

Clarifying questions to ask

  1. Case-sensitive or not?
  2. Should spaces and punctuation be ignored? ("A man, a plan, a canal: Panama")
  3. Is recursion required?
  4. How long can the string be? (Recursion depth.)

Follow-ups to expect

  1. "What are the base cases?"
  2. "How would you ignore case and punctuation?"
  3. "Why pass indices instead of substrings?"
  4. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in