🏷️ Category: Strings  |  🎯 Difficulty: Easy  |  πŸ”₯ Frequency: β˜…β˜…β˜…β˜…β˜…


πŸ“‹ Problem Statement

Given a string, return True if it reads the same forwards and backwards, else False. Ignore case and non-alphanumeric characters.

πŸ“₯ Input  β†’ "racecar"                       πŸ“€ Output β†’ True
πŸ“₯ Input  β†’ "A man a plan a canal Panama"   πŸ“€ Output β†’ True
πŸ“₯ Input  β†’ "hello"                         πŸ“€ Output β†’ False

🧠 What the Interviewer is Testing

ConceptWhy It Matters
String cleaningReal-world input handling
isalnum() methodPython string methods
Comparison with reversePythonic thinking

πŸ’‘ Approach

  1. Clean the string β€” lowercase + keep only alphanumeric characters
  2. Compare cleaned string with its reverse
"A man a plan a canal Panama"
β†’ clean β†’ "amanaplanacanalpanama"
β†’ reverse β†’ "amanaplanacanalpanama"
β†’ equal? βœ… True

βœ… Optimal Solution

def is_palindrome(s: str) -> bool:
    # Step 1: Clean β€” lowercase + alphanumeric only
    cleaned = ''.join(c.lower() for c in s if c.isalnum())

    # Step 2: Compare with its reverse
    return cleaned == cleaned[::-1]

# ── Test Cases ─────────────────────────────────
print(is_palindrome("racecar"))                        # βœ… True
print(is_palindrome("A man a plan a canal Panama"))    # βœ… True
print(is_palindrome("hello"))                          # βœ… False
print(is_palindrome(""))                               # βœ… True (empty is palindrome)

πŸ”„ Two Pointer Alternative

def is_palindrome_two_ptr(s: str) -> bool:
    cleaned = ''.join(c.lower() for c in s if c.isalnum())
    left, right = 0, len(cleaned) - 1

    while left < right:
        if cleaned[left] != cleaned[right]:
            return False
        left += 1
        right -= 1
    return True

πŸ’¬ Pro tip: Mention the two-pointer approach as an alternative β€” it shows deeper understanding and O(1) space awareness.


πŸ“Š Complexity Analysis

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Time  O(n)  β”‚ Clean + reverse both are O(n)      β”‚
β”‚ Space O(n)  β”‚ Cleaned string stored in memory    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🎀 How to Explain in the Interview

β€œFirst I clean the string β€” convert to lowercase and keep only alphanumeric characters. Then I compare it with its reverse. If they’re equal, it’s a palindrome. I can also use a two-pointer approach which avoids creating the reversed string.”


πŸ” Common Follow-up Questions

Q1 β†’ What about numbers like 121?

def is_palindrome_num(n: int) -> bool:
    s = str(n)
    return s == s[::-1]

Q2 β†’ Without converting to string?

# Math approach β€” reverse the number
def is_palindrome_math(n: int) -> bool:
    if n < 0:
        return False
    original, reversed_n = n, 0
    while n > 0:
        reversed_n = reversed_n * 10 + n % 10
        n //= 10
    return original == reversed_n

πŸ“ Set 1 of 4 β€” Python Interview Prep Series β€” devprepguide.com

Scroll to Top