Skip to main content

17. Valid Palindrome

easyAsked at Salesforce

Determine if a string is a palindrome considering only alphanumeric characters and ignoring case. Salesforce uses this to test two-pointer string manipulation with edge-case handling.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Common warmup on Salesforce frontend phone screens.
  • LeetCode Discuss (2025-09)Used to test string normalization fluency.

Problem

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Given a string s, return true if it is a palindrome, or false otherwise.

Constraints

  • 1 <= s.length <= 2 * 10^5
  • s consists only of printable ASCII characters.

Examples

Example 1

Input
s = "A man, a plan, a canal: Panama"
Output
true

Example 2

Input
s = "race a car"
Output
false

Example 3

Input
s = " "
Output
true

Approaches

1. Filter then reverse-compare

Filter alphanumerics, lowercase, compare to its reverse.

Time
O(n)
Space
O(n)
function isPalindrome(s) {
  const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g, '');
  return cleaned === [...cleaned].reverse().join('');
}

Tradeoff: Easy to read but allocates a new string. Salesforce will push for in-place two-pointer.

2. Two-pointer in place

Pointers from both ends. Skip non-alphanumerics. Compare lowercased values. Move inward.

Time
O(n)
Space
O(1)
function isPalindrome(s) {
  const isAlnum = (c) => /[a-z0-9]/i.test(c);
  let l = 0, r = s.length - 1;
  while (l < r) {
    while (l < r && !isAlnum(s[l])) l++;
    while (l < r && !isAlnum(s[r])) r--;
    if (s[l].toLowerCase() !== s[r].toLowerCase()) return false;
    l++; r--;
  }
  return true;
}

Tradeoff: O(1) extra space. The inner while-loops handle arbitrary-length runs of non-alphanumerics.

Salesforce-specific tips

Salesforce values fluency with string normalization (lowercase, alphanumeric filter) because their SOQL identifier matching uses similar rules. Bonus signal: mention that the two-pointer version short-circuits on the first mismatch, which is faster than allocating a full cleaned string and reversing.

Common mistakes

  • Forgetting l < r inside the inner while-loops — pointers cross when the string is all non-alphanumeric.
  • Comparing without lowercasing — fails 'A man, a plan...' case.
  • Using ASCII range checks instead of regex — works but is brittle to non-ASCII inputs.

Follow-up questions

An interviewer at Salesforce may pivot to one of these next:

  • Valid Palindrome II (LC 680) — allow one deletion.
  • Longest Palindromic Substring (LC 5).
  • Palindrome Linked List (LC 234).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why not just use String.prototype.normalize?

normalize handles Unicode equivalence but not alphanumeric filtering. The interview wants you to demonstrate the explicit two-pointer logic.

What if the string has Unicode like 'café'?

The regex /[a-z0-9]/i excludes Unicode letters. For real Unicode support, use Intl.Collator or normalize + a broader regex. Mention this as a follow-up consideration.

Practice these live with InterviewChamp.AI

Drill Valid Palindrome and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →