# The Mirror Words

> Each word looks back at itself.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a sentence, reverse the characters in each space-separated word, preserving word order and single-space separators.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **string splitting and per-word transformation**. It checks whether candidates can decompose a problem into split, transform, and rejoin steps without overcomplicating the logic.

---

### Break down the requirements

#### Step 1: Split the sentence into words

Use `split(' ')` to separate words by spaces.

#### Step 2: Reverse each word individually

For each word, reverse its characters. Use the two-pointer approach or build the reversed string.

#### Step 3: Rejoin with spaces

Combine the reversed words back into a single string with spaces between them.

---

### The solution

**Split, reverse each word, rejoin**

```python
def reverse_words(sentence):
    words = sentence.split(' ')
    reversed_words = []
    for word in words:
        chars = list(word)
        left = 0
        right = len(chars) - 1
        while left < right:
            chars[left], chars[right] = chars[right], chars[left]
            left += 1
            right -= 1
        reversed_words.append(''.join(chars))
    result = ' '.join(reversed_words)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the total number of characters. Each character is processed once during reversal.
> 
> **Space:** O(n) for the output string and intermediate character lists.

> **Interviewers Watch For**
>
> Do you keep the words in their original positions? The problem only reverses characters within each word. Candidates who reverse word order instead have misread the requirements.

> **Common Pitfall**
>
> Using `[::-1]` for reversal. While it works, interviewers may specifically want to see the two-pointer approach to verify understanding of the mechanics.

---

## Common follow-up questions

- What if the sentence had multiple consecutive spaces? _(Tests whether split behavior changes and how to preserve spacing.)_
- How would you reverse the order of words instead of their characters? _(Tests the complementary problem: split, reverse the list, rejoin.)_
- Can you do this in-place on a character array? _(Tests the classic two-step: reverse the whole array, then reverse each word.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_mirror_words)
- [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.