# The Mirror Test

> Check if a string reads the same forwards and backwards.

Canonical URL: <https://datadriven.io/problems/the_mirror_test>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a string, return True if it reads the same forwards and backwards when considering only alphanumeric characters and ignoring case. Empty string returns True.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **two-pointer string comparison with filtering**. It goes beyond simple reversal by requiring candidates to skip non-alphanumeric characters and ignore case, testing both pointer management and character classification.

---

### Break down the requirements

#### Step 1: Set up two pointers at opposite ends

Left pointer starts at 0, right pointer at the last index.

#### Step 2: Skip non-alphanumeric characters

Advance each pointer past spaces, punctuation, and symbols. Only compare alphanumeric characters.

#### Step 3: Compare characters case-insensitively

Convert both characters to lowercase before comparing. If they differ, the string is not a palindrome.

#### Step 4: Handle the empty string

An empty string is defined as a palindrome.

---

### The solution

**Two-pointer with alphanumeric filtering**

```python
def is_palindrome(s):
    left = 0
    right = len(s) - 1
    while left < right:
        while left < right and not s[left].isalnum():
            left += 1
        while left < right and not s[right].isalnum():
            right -= 1
        if s[left].lower() != s[right].lower():
            return False
        left += 1
        right -= 1
    return True
```

> **Time and Space Complexity**
>
> **Time:** O(n) with a single pass from both ends. Each character is examined at most once.
> 
> **Space:** O(1). No extra data structures beyond the two pointers.

> **Interviewers Watch For**
>
> Do you skip non-alphanumeric characters in both inner loops while maintaining the `left < right` guard? Missing the guard in the inner while loops causes an index out of bounds on strings like `'!!!'`.

> **Common Pitfall**
>
> Pre-filtering the string into a cleaned version and then checking if it equals its reverse. This uses O(n) extra space and is less elegant than the in-place two-pointer approach.

---

## Common follow-up questions

- What if the palindrome check should also ignore whitespace but be case-sensitive? _(Tests modifying the filter condition while keeping the structure.)_
- How would you find the longest palindromic substring? _(Tests the expand-around-center technique or dynamic programming.)_
- What if the input were a number and you needed to check without converting to string? _(Tests arithmetic palindrome checking with digit extraction.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_mirror_test)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.