Reverse String
Maintained by Vikas D, software engineer
You are given a string `s`. Build and return the string you get when its characters are read from the last position to the first.
Order is the only thing that changes. Every character that was present stays present, including spaces, punctuation, and repeated letters, and the length of the result equals the length of the input.
An empty string has nothing to flip, so it comes back unchanged, and a single character reads the same forwards and backwards. Casing is preserved exactly, so an uppercase letter stays uppercase in its new spot.
Examples
Reading h-e-l-l-o from the back gives o-l-l-e-h.
The space and the capital letters keep their characters but land in mirrored positions: the trailing 'd' becomes the first character and the leading 'A' becomes the last.
There are no characters to reorder, so the empty string maps to itself.
Constraints
- 0 <= s.length
- `s` may contain any characters: letters, digits, spaces, and punctuation.
- Return a new string; the comparison is on the returned value.
Visible test cases
- reverseString("hello") == "olleh"
- reverseString("") == ""
- reverseString("a") == "a"
- reverseString("Ace Hired") == "deriH ecA"
Hints
Hint 1
If you could walk the string from its last index down to index 0 and collect each character, what would you have built?
Hint 2
Strings are awkward to mutate character by character in many languages. Convert to an array of characters first, do the work, then join back.
Hint 3
You do not need a second array. Swap the character at the left end with the one at the right end, then step both pointers inward until they meet.
Approaches
Build by reverse traversal
Time O(n) · Space O(n)Read the input from the final character to the first and append each one to a result you grow as you go.
- Start with an empty accumulator (an array of characters works well).
- Loop the index from s.length - 1 down to 0.
- Append s at that index to the accumulator.
- Join the accumulator into a string and return it.
Two-pointer in-place swap
Time O(n) · Space O(n)Convert the string to a character array, then mirror it by swapping the outermost pair and moving inward until the pointers cross.
- Split the string into an array of characters.
- Set a left pointer at index 0 and a right pointer at the last index.
- While left is less than right, swap the two characters, then increment left and decrement right.
- Join the array back into a string and return it.
Common mistakes
- Reaching for a language built-in and forgetting the task is to understand the reversal itself; an interviewer usually wants the two-pointer or manual loop.
- Off-by-one on the loop bound: starting from s.length instead of s.length - 1 reads past the end, and stopping at left <= right does a wasteful extra swap that can undo the middle character on odd lengths.
- Trying to mutate a string in place in languages where strings are immutable (JavaScript, Python); you must work on an array or a fresh string.
- Dropping spaces or punctuation by filtering; every character is part of the output, only the order changes.
- Assuming the input is non-empty and indexing s[0] without guarding; the empty string must return the empty string.
Follow-up variations
- Reverse only the letters and leave every non-letter character fixed in its original position.
- Reverse the order of words while keeping each word's internal characters in place.
- Handle full Unicode correctly so that multi-code-unit characters and combining marks are not split apart.
- Do the reversal as a true in-place operation when the input is given as a mutable character array rather than a string.