Loading…
Valid Palindrome is a easy coding interview question covering strings, two pointers — the kind asked at FAANG (Facebook/Meta, Apple, Amazon, Netflix, Google) and AI-first companies like Anthropic, OpenAI, and Nvidia.
Given a string s, return true if it is a palindrome considering only alphanumeric characters and ignoring case. Example: "A man, a plan, a canal: Panama" -> true; "race a car" -> false.
Try it yourself first
In an interview you'd get about 15 minutes for a easy question. Sketch an approach before peeking — struggling is where the learning happens.
Stuck? Reveal them one at a time — each hint gives away a little more.
Ready to compare notes? The full solution, visualization, and complexity analysis are below.
Given a string, decide whether it reads the same forward and backward considering only alphanumeric characters and ignoring case. The canonical warm-up for two-pointer technique: one pointer walks from each end, skipping everything that isn't a letter or digit, comparing lowercase forms as the pointers meet in the middle.
| 1 | def is_palindrome(s: str) -> bool: |
| ▶ | left, right = 0, len(s) - 1 |
| 3 | while left < right: |
| 4 | if not s[left].isalnum(): |
| 5 | left += 1 |
| 6 | elif not s[right].isalnum(): |
| 7 | right -= 1 |
| ▶ | elif s[left].lower() != s[right].lower(): |
| 9 | return False |
| 10 | else: |
| ▶ | left += 1 |
| ▶ | right -= 1 |
| 13 | return True |
Pointers start at both ends: left on "A", right on "a" (last char). Both alphanumeric, lowercase forms "a" == "a" — match, move both inward.
Key Insights
A full interview loop is more than one round. These are the others, each with what is on the site for it.