Validate normalized palindromes with variants
Company: xAI
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates string-processing skills including Unicode normalization, case-folding and diacritics handling, streaming input processing, error-tolerant palindrome logic, and analysis of time/space trade-offs.
Part 1: Basic normalized palindrome
Constraints
- 0 <= len(s) <= 200000
- s may contain letters, digits, spaces, punctuation, and Unicode characters
- Use Unicode-aware alphanumeric checks and case folding
Examples
Input: ("A man, a plan, a canal: Panama!",)
Expected Output: True
Explanation: Normalization gives 'amanaplanacanalpanama', which is a palindrome.
Input: ("race a car",)
Expected Output: False
Explanation: Normalization gives 'raceacar', which is not a palindrome.
Hints
- Build the normalized sequence first: keep only alphanumeric characters and apply case folding.
- After normalization, palindrome checking is just a compare against the reversed sequence.
Part 2: Unicode-normalized palindrome with diacritics stripping and locale rules
Constraints
- 0 <= len(s) <= 100000
- locale_code is one of 'default', 'tr', or 'az'
- Use NFKD normalization and strip combining marks with Unicode-aware logic
Examples
Input: ("Noël, Léon", "default")
Expected Output: True
Explanation: After case folding, NFKD normalization, and diacritics stripping, the string becomes 'noelleon', which is a palindrome.
Input: ("I, ı", "tr")
Expected Output: True
Explanation: In Turkish, 'I' maps to 'ı', so the normalized form is 'ıı'.
Hints
- The Turkish/Azeri dotted-I rule must be handled before generic case folding, otherwise 'I' and 'İ' lose their distinction.
- Use unicodedata.normalize('NFKD', ...) and skip characters where unicodedata.combining(ch) is nonzero.
Part 3: Streaming normalized palindrome with one pass and O(1) extra space
Constraints
- The total number of characters across all chunks can be very large
- Each chunk must be processed in order and should not require storing the full normalized string
- Use O(1) extra space beyond a constant number of integers
Examples
Input: (["A man, ", "a plan,", " a canal: Panama"],)
Expected Output: True
Explanation: The normalized stream is 'amanaplanacanalpanama'.
Input: (["race", " a car"],)
Expected Output: False
Explanation: The normalized stream is 'raceacar', which is not a palindrome.
Hints
- Maintain both a forward polynomial hash and a reverse-position polynomial hash as characters arrive.
- Use two different moduli to make collisions extremely unlikely.
Part 4: K-deletion normalized palindrome
Constraints
- 0 <= len(s) <= 2000 after normalization
- 0 <= k <= 2000
- An O(n^2) dynamic programming solution is expected
Examples
Input: ("abca", 1)
Expected Output: True
Explanation: Deleting 'b' or 'c' produces a palindrome.
Input: ("abc", 1)
Expected Output: False
Explanation: At least two deletions are needed.
Hints
- Think in terms of the minimum number of deletions needed to make a substring a palindrome.
- Equivalent viewpoint: minimum deletions = length - longest palindromic subsequence.
Part 5: Return the first mismatch index pair
Constraints
- 0 <= len(s) <= 200000
- Indices in the answer refer to positions in the original input string
- Use Unicode-aware alphanumeric checks and case folding
Examples
Input: ("ab!ca",)
Expected Output: (1, 3)
Explanation: Normalized form is 'abca'; the first mismatch is 'b' vs 'c', from original indices 1 and 3.
Input: ("A man, a plan, a canal: Panama!",)
Expected Output: (-1, -1)
Explanation: The normalized string is a palindrome.
Hints
- Store both the normalized characters and the original index each normalized character came from.
- Then run a normal two-pointer palindrome check on the normalized list and return the mapped original indices at the first mismatch.