Reverse a string

s[::-1] is the idiomatic answer and allocates a full copy. The follow-up is always "now in place", which strings cannot do — so you convert to a list and run two pointers inwards, swapping as they go: n/2 swaps and O(1) extra memory.

Overview

The Python answer, and why it is not the whole answer

s[::-1] is correct, fast and what you should write in real code. It also allocates a second string of the same length, because Python strings are immutable and there is no in-place option. An interviewer asking for O(1) space is asking you to leave strings behind and work on a character array.

StringsCoding problemEasy

Step through it

What to watch

  • The two pointers move towards each other and stop when they meet.
  • An odd-length string leaves the middle character alone — correct, not a bug.
  • Nothing is allocated: the same list is being rearranged.

Say this out loud

"s[::-1] in Python, but that's O(n) space. In place you'd take a character array and swap from both ends inwards until the pointers meet - n/2 swaps, O(1) extra."

Reverse a string

Reverse a string. Now do it in place, with O(1) extra memory.

The two-pointer version

Put one pointer at each end. Swap the characters they name, move both inwards, stop when they meet. That is n/2 swaps and two integers of extra memory, regardless of length.

The loop condition is while lo < hi, strictly less than. With <= an odd-length string swaps its middle character with itself — harmless but pointless, and the condition is the part interviewers check.

The variations that follow

"Reverse the words but not the characters" — " ".join(s.split()[::-1]), or reverse the whole thing and then reverse each word in place, which is the O(1)-space trick.

"Reverse only the vowels" — the same two pointers, each skipping forward until it lands on a vowel. "Reverse in groups of k" — the same swap loop applied to each block.

Run it in Python

Three reversals of the same text — the slice, the two-pointer swap, and the word-level variant — with the extra memory each one uses printed beside it.

reverse.pyPython 3
Output

How the code works

  1. while lo < hi:Strictly less than. With <= an odd-length string swaps its middle character with itself — harmless, and the kind of detail the question exists to check.
  2. chars[lo], chars[hi] = chars[hi], chars[lo]Python evaluates the right side first, so this is a real swap. In most languages it needs a temporary, and forgetting it overwrites one of the two values.
  3. chars = list(s)The concession immutability forces. There is no in-place reversal of a str; "in place" means in a character array.
  4. while lo < hi and chars[lo] not in vowels:The inner skips need the lo < hi guard too, or a string with no vowels runs a pointer off the end. Nested pointer loops are where index errors hide.

Change one thing

  • Change the loop to while lo <= hi and print the swap count for an odd-length string. One wasted swap, same output.
  • Reverse the words with O(1) extra space: reverse the whole array, then reverse each word in place. That is the real interview follow-up.

Where this runs

Real CPython, compiled to WebAssembly and running on your own machine — nothing is uploaded. The first run takes a few seconds while the interpreter downloads; after that it is immediate. Need more room, or want to paste your own attempt? Use the Python compiler.

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why can't you reverse a Python string in place?

  2. The two-pointer reversal loop uses `while lo < hi` rather than `<=` because:

  3. How many swaps does the in-place reversal make for n characters?

Cheat sheet

Reverse a string

s[::-1] is the idiomatic answer and allocates a full copy. The follow-up is always "now in place", which strings cannot do — so you convert to a list and run two pointers inwards, swapping as they go: n/2 swaps and O(1) extra memory.

INTERVIEW · vizlearn.in/interview/reverse-a-string.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.