# Reverse Field

> Flip it. See what happens.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a string s, return its reverse. Input is guaranteed to be a non-None string (may be empty).

## Worked solution and explanation

### Why this problem exists in real interviews

String reverse is a warm-up that interviewers use to probe Python fluency before harder questions. The right answer is one line; the wrong answer is a five-line for-loop with index arithmetic. What they actually grade is whether you know the slice idiom, whether you handle empty input without thinking, and whether you can talk about Unicode caveats when pressed.

---

### Break down the requirements

#### Step 1: Use the slice idiom

s[::-1] is the canonical Python reverse. It is one expression, runs in C, and works on any sliceable sequence. Anything longer is either showing off or missing the idiom.

#### Step 2: Handle empty input as a no-op

''[::-1] returns '', so no special case is needed. The contract guarantees a non-None string, so you can skip None-checks too. Naming this out loud signals that you read the spec.

#### Step 3: Acknowledge Unicode caveats

For ASCII this is trivial, but combining characters and emoji modifiers can split apart under naive reversal. The spec scope is bytes-level reverse; mention the grapheme-cluster nuance to show senior thinking without over-engineering the answer.

---

### The solution

**One slice does it**

```python
def reverse_str(s: str) -> str:
    return s[::-1]
```

> **Cost Analysis**
>
> Time is O(n) where n is the length of the string. Space is O(n) for the new string (Python strings are immutable, so a new object is allocated). The slice form runs in C and beats any Python-level loop by a wide margin.

> **Interviewers Watch For**
>
> Whether you write the slice immediately or wander into ''.join(reversed(s)), and whether you can defend O(n) time and space without hesitating. Strong candidates also raise the grapheme-cluster point unprompted, which signals they have shipped i18n code.

> **Common Pitfall**
>
> Calling s.reverse(), which does not exist on strings (only lists), and returning None implicitly. Another miss is reaching for a manual loop with range(len(s) - 1, -1, -1) and concatenating with += inside the loop, which is O(n^2) due to Python string immutability.

---

## Common follow-up questions

- How would you reverse only the words in a sentence, keeping word order intact within each word? _(split on whitespace, slice-reverse each token, rejoin; discuss multiple-space preservation.)_
- What changes for a Unicode string with combining marks or emoji modifiers? _(mention the regex grapheme-cluster pattern \X (in regex package) or the unicodedata module.)_
- How would you reverse a 10 GB string that does not fit in memory? _(stream from the end of a file with seek/read, or memory-map it; discuss chunked reverse with byte-boundary care.)_

## Related

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