DSA interview question
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.
bool isPalindrome(const string& s, int left, int right); // call with (s, 0, s.size() - 1)
Input s | Output | Explanation |
|---|---|---|
"abba" | true | Even length |
"racecar" | true | Odd length; the middle character is its own mirror |
"abc" | false | 'a' ≠ 'c' at the first comparison |
"a" | true | A single character |
"" | true | The empty string reads the same both ways |
"Racecar" | false | Case-sensitive: 'R' ≠ 'r' |
"abca" | false | Ends 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.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in