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.
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."