Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Two pointers from the ends: if `s[left] != s[right]` return false; otherwise converge. If they cross, it's a palindrome. O(n) time, O(1) space.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you use the O(1)-space two-pointer symmetry check.
Where candidates slip
Reverse-and-compare with extra space, or overshooting pointers.
How to stand out
Mention the case/alphanumeric-insensitive variant (LC125).
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Two-pointer compare
1Hint 1Explore hintHide hint
What relationship must the i-th front and i-th back characters have?
2Hint 2Explore hintHide hint
Two pointers inward; mismatch ⇒ not a palindrome.
3Hint 3Explore hintHide hint
Avoid building a reversed copy — two pointers are O(1) space.
A · Core idea
A palindrome is symmetric about its centre, so the i-th character from the front must equal the i-th from the back. Compare inward with two pointers and bail on the first mismatch — O(n) time, O(1) space.
Why not build a reversed copy. Reversing and comparing works but uses O(n) extra space and always scans the whole string. The two-pointer check is O(1) space and can exit early on the first mismatched pair.
Why the pointers meet, not cross. With left < right, the middle character of an odd-length string is never compared against itself (it's trivially equal), and even-length strings compare every pair exactly once.
Common real-world variant. 'Valid palindrome' often ignores case and non-alphanumeric characters — there you skip non-letters and lower-case before comparing. This basic version compares all characters as-is.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "madam"l=0,r=4
m == m
oktrueC · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(1) space — at most n/2 comparisons, with early exit on the first mismatch, and only two index variables.
E · Edge cases and pitfalls5s = "a" — Single char → true.Explore edge casesHide edge cases
Edge cases handled
s = "a"Single char → true.s = ""Empty → true by convention.s = "ab"a ≠ b → false.
Pitfalls
- Building a reversed string (O(n) space) instead of two pointers.
- Off-by-one letting the pointers overshoot.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while (left < right) { if (s.charAt(left) != s.charAt(right)) return false; left++;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Building a reversed string (O(n) space) when two pointers are O(1).
- Off-by-one letting the pointers overshoot.
Revise
Carry the pattern into the interview
Pattern to remember
Two-pointer symmetry check
Code pattern to memorize
l,r=0,n-1; while l<r: if s[l]!=s[r] return false; l++; r--Rules to remember
- 1Compare ends inward.
- 2Mismatch ⟹ false.
- 3O(1) space.
Use this pattern when
- Symmetry checks
- Two-pointer string scans
Interview takeaway
Palindrome checks are symmetry — compare from both ends.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Two-pointer symmetry check. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Valid PalindromeComing soonIgnore case and non-alphanumerics while two-pointer scanning.
- Palindrome NumberSame symmetry idea on digits.