Reverse a Python string without [::-1]. Learn why += in a loop can be O(n²) and why ''.join on a list is the O(n) answer interviewers want.
The problem
Return s reversed without using slicing ([::-1]) or reversed().
Examples
Example 1
Input
reverse_string('python')Expected output
'nohtyp'
Example 2
Input
reverse_string('ab')Expected output
'ba'
+ 3 hidden tests on Submit.
Edge cases to ask about
- Empty string
- Single character
- Palindrome
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.
Pick both to reveal the answer.
Measure it
Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Prepend in a loop | O(n²) | O(n) | Strings are immutable: every ch + out copies the whole result so far. |
| bestSwap in a list, join once | O(n) | O(n) | Lists are mutable; one join at the end. |
Walkthrough of the optimal approach (try it yourself first)
Strings are immutable, so convert to a list, swap characters from the two ends inward, and "".join() once at the end.
The out = ch + out loop is the classic trap: each concatenation copies the whole string, so it is O(n²). (CPython sometimes optimises out += ch, but prepending defeats it.)
Complexity: O(n) time, O(n) space. Converting to a list and joining are linear; the list is the extra O(n) memory a new string needs anyway.
Reveal the reference solution
def reverse_string(s): chars = list(s) i, j = 0, len(chars) - 1 while i < j: chars[i], chars[j] = chars[j], chars[i] i += 1 j -= 1 return "".join(chars)
The brute force, for comparison
def reverse_string(s): out = "" for ch in s: out = ch + out # builds a new string every time return out
Follow-ups interviewers ask
- Reverse the order of words, not characters.
- Do it recursively (question 50).
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Reverse a String Without Slicing in Python?
The optimal solution runs in O(n) time and O(n) auxiliary space. Converting to a list and joining are linear; the list is the extra O(n) memory a new string needs anyway.
What is the brute-force approach, and how do you optimise it?
Prepend in a loop: O(n²) time, O(n) space. Strings are immutable: every `ch + out` copies the whole result so far. Swap in a list, join once: O(n) time, O(n) space. Lists are mutable; one join at the end.
What follow-up questions do interviewers ask about Reverse a String Without Slicing?
Reverse the order of words, not characters. Do it recursively (question 50).
