← Questions

Valid palindrome and its variants

0
SpaceXAICodingLast asked Jul 2025

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 İ → i before 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 most k characters 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 original s of the first pair that fails, or None.
first_mismatch("race a car")  ->  (3, 5)    # "e" against "a"
first_mismatch("Racecar")     ->  None

Constraints

  • len(s) up to 2 * 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 casefold beats lower (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 k deletions, the DP on the longest palindromic subsequence, or a recursion bounded by k, 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

What it tests

strings · two pointers

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding