# The Mirror Image

> Flip the tape backwards - start from the end.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a string s, return its reverse. Do not use slice reversal or reversed(); implement with a two-pointer or manual character-by-character approach.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests the **two-pointer technique** applied to string reversal. By prohibiting `[::-1]` and `reversed()`, interviewers force candidates to demonstrate understanding of in-place swapping mechanics.

---

### Break down the requirements

#### Step 1: Convert string to a mutable list

Python strings are immutable, so convert to a list of characters to enable swapping.

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

Pointer `left` starts at index 0, pointer `right` starts at the last index.

#### Step 3: Swap and move inward

Swap the characters at `left` and `right`, then increment `left` and decrement `right`. Stop when they meet.

#### Step 4: Join and return

Convert the list back to a string with `''.join()`.

---

### The solution

**Two-pointer swap reversal**

```python
def reverse_string(s):
    chars = list(s)
    left = 0
    right = len(chars) - 1
    while left < right:
        chars[left], chars[right] = chars[right], chars[left]
        left += 1
        right -= 1
    result = ''.join(chars)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) with n/2 swaps.
> 
> **Space:** O(n) for the character list. Strings are immutable in Python, so a copy is unavoidable.

> **Interviewers Watch For**
>
> Do you mention that Python strings are immutable? This shows language-level awareness. In languages with mutable strings, the space could be O(1).

> **Common Pitfall**
>
> Using `left <= right` instead of `left < right`. When `left == right`, the middle character does not need swapping, but it does not cause a bug. Using `<` is slightly cleaner.

---

## Common follow-up questions

- How would you reverse only a substring from index i to j? _(Tests applying the two-pointer technique to a bounded range.)_
- What if you needed to reverse words but not characters? _(Tests the 'reverse all, then reverse each word' pattern.)_
- Can you do this without converting to a list? _(Tests building the string from the end using concatenation or a list of characters appended in reverse order.)_

## Related

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