Valid palindrome and its variants
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in SpaceXAI's technical phone screen, last asked Jul 2025. It tests strings and two pointers.
Decide whether a string is a palindrome after removing every character that isn't a letter or digit and folding case. Then extend it step by step: Unicode normalization, streaming input, deletions, and reporting where it fails.
Part 1: normalized palindrome
Unless you agree on a different interface with the interviewer, assume:
-
is_normalized_palindrome(s: str) -> bool— keep only alphanumeric characters (Unicode-aware), case-fold, compare. An empty result is a palindrome.
is_normalized_palindrome("A man, a plan, a canal: Panama!") -> True
is_normalized_palindrome("race a car") -> False
is_normalized_palindrome(".,") -> True
Part 2: Unicode normalization and locale rules
-
is_unicode_palindrome(s: str, locale: str = "default") -> bool— apply locale case rules (for"tr"and"az":I→ıandİ→ibefore folding), case-fold, normalize to NFKD, strip combining marks, keep alphanumerics, compare.
is_unicode_palindrome("Noël, Léon") -> True # becomes "noelleon"
is_unicode_palindrome("I, ı", "tr") -> True # becomes "ıı"
Part 3: streaming input
The text arrives as a stream of chunks and may not fit in memory. Check it in one pass with sublinear extra space, and say what that costs (for example a rolling hash compared forwards and backwards, which can give false positives).
Part 4: deletions
-
valid_with_deletions(s: str, k: int = 1) -> bool— true if deleting at mostkcharacters makes it a palindrome.
valid_with_deletions("abca", 1) -> True # delete "b" or "c"
valid_with_deletions("abc", 1) -> False
Part 5: first mismatch
-
first_mismatch(s: str) -> tuple[int, int] | None— indices into the originalsof the first pair that fails, orNone.
first_mismatch("race a car") -> (3, 5) # "e" against "a"
first_mismatch("Racecar") -> None
Constraints
-
len(s)up to2 * 10^5.
Clarifying questions
- What counts as alphanumeric: ASCII only or Unicode?
- Case folding or lowercasing?
- Is an empty string a palindrome?
- For deletions, do we delete from the original or the normalized string?
What a strong answer covers
- Two pointers that skip non-alphanumeric characters and compare folded characters, O(n) time and O(1) extra space, without building a cleaned copy.
- Why
casefoldbeatslower(Germanß), and why the Turkish rule must come before folding. - The one-deletion version: at the first mismatch, try skipping either side once; O(n). For
kdeletions, the DP on the longest palindromic subsequence, or a recursion bounded byk, with their costs. - Honest limits of the streaming version.
- Unit tests for empty strings, all punctuation, Unicode cases and the locale rule.
Follow-ups
- Find the longest palindromic substring.
- Return the deletion that fixes the palindrome.
- Check palindromes over words rather than characters.
Where it's asked
- SpaceXAI · Technical phone screen · Last asked Jul 202545 min · Live applied coding on a shared platform such as CoderPad · No AI
What it tests
strings · two pointers
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
