Palindrome Check Two Pointers Java | Fresher Interview
Fast recall
Quick revision
Core answer
A simple two-pointer application that verifies you know the technique before harder variants like three-sum
Reversing the string and comparing (O(n) space) without knowing the O(1) two-pointer approach, or not handling case and non-alphanumeric characters
Offer both approaches, prefer the O(1) two-pointer, and handle the edge case of skipping non-alphanumeric characters and case
Interview-ready guide
Interview answer
To check if a string is a palindrome using two pointers, you start with one pointer at the beginning and one at the end, then move them toward each other while comparing characters.
If they ever don't match, it's not a palindrome. If you get all the way to the middle without a mismatch, it is a palindrome. Here's how it works in detail.
Initialize left to zero and right to s dot length minus one. While left is less than right, compare s dot charAt left with s dot charAt right. If they're not equal, return false immediately.
If they match, increment left and decrement right, then loop again. When left meets or passes right, you've verified all symmetric pairs and return true. This runs in O of n time because you visit each character once, and O of one space because you only use two integer variables.
The common follow-up is handling non-alphanumeric characters and case, like treating A man a plan a canal Panama as a valid palindrome. For this, you add inner while-loops that skip characters where Character dot isLetterOrDigit returns false, and you wrap both characters in Character dot toLowerCase before comparing. Reverse-and-compare with StringBuilder also works, but it creates another O(n)-sized string.
Comparing the symmetric pairs directly keeps the same O(n) running time while using only O(1) extra space.
Palindrome Check Two Pointers Java | Fresher Interview
Direct Answer
Use two pointers, one at each end, moving inward. If the characters match, move both inward; on a mismatch it's not a palindrome. If the pointers cross without a mismatch, it is a palindrome. This is O(n) time, O(1) space.
Deep Dive & Architecture
A palindrome has matching characters at equal distances from the two ends. Put left at the start and right at the end, compare the pair, and move both inward. One mismatch is enough to return false.
Worked example: level
Compare l with l, then e with e. The pointers meet at v, which needs no partner, so the string is a palindrome. For lever, the first pair l and r differs, so the method stops immediately.
This direct comparison is O(n) time and O(1) extra space. Reversing the string and comparing it is also O(n), but it creates another O(n)-sized object.
Clarify the matching rules. A simple version is case-sensitive and includes every character. A phrase version may skip non-alphanumeric characters and compare lowercase forms, making A man, a plan, a canal: Panama valid. Those are separate requirements and should not be assumed silently.
In Java, charAt compares UTF-16 code units. That is enough for common interview ASCII input. If arbitrary Unicode symbols are in scope, compare code points so a supplementary character is not split into two independent units.
Code Example
public class PalindromeChecker {
public boolean isPalindrome(String s) {
if (s == null || s.isEmpty()) return true;
int left = 0, right = s.length() - 1;
while (left < right) {
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) left++;
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) right--;
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right)))
return false; // mismatch
left++;
right--;
}
return true; // all pairs matched
}
public static void main(String[] args) {
System.out.println(new PalindromeChecker()
.isPalindrome("A man, a plan, a canal: Panama")); // true
}
}
Common Mistakes & Pitfalls
- Only knowing reverse-and-compare:
s.equals(new StringBuilder(s).reverse().toString())works but uses O(n) space — interviewers want the O(1) two-pointer. - Forgetting non-alphanumeric filtering: when asked to ignore spaces and punctuation, add inner while-loops to skip invalid characters.
- Case-sensitivity bugs: normalize with
Character.toLowerCaseon both characters unless the problem is case-sensitive.
Check your understanding
Answer these without looking back at the article.
- What is the two-pointer technique and when do you use it?
- How do you remove duplicates from a sorted array in-place?
- How do you use two pointers to find a pair with a given sum in a sorted array?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.