Strings · Easy
46. Reverse a String
Topics: String
Solve it on DeVert Campus - free code runner
Problem
Given a string s, print it reversed.
Input and output
Input: The input is a single line containing the string s.
Output: Print s reversed on a single line.
Example
Input: hello Output: olleh
Constraints
1 <= |s| <= 10^5
Key observation
In languages where strings are immutable (like Java or Python), true 'in-place' reversal isn't literally possible without converting to a mutable array first - worth mentioning if asked about space complexity precisely.
Intuition
Flip a string so its last character becomes the first, and so on backward.
Brute force: Build a brand new string by reading the original back to front and appending each character - correct, and in many languages this is even the natural one-liner, but it's worth understanding the in-place pointer technique underneath.
Optimised: Two pointers moving toward each other from both ends, swapping as they go, reverses the string using its own storage (when the string type is mutable, like a char array) - no separate second string needed.
Better - Auxiliary Stack
Push every character of the string onto a stack one at a time, then pop characters off one by one, appending each popped character to a mutable output buffer. Since a stack is last-in-first-out, the last character pushed (the string's final character) is the first one popped, which naturally produces the characters in reverse order without the repeated full-string copies the brute approach required.
Time: O(n)
Space: O(n)
Optimal - Two-Pointer In-Place Swap
Convert the string into a mutable array of characters, then use two pointers - one starting at the front, one at the back - repeatedly swapping the characters they point to and moving both pointers toward the middle until they cross. This reverses the whole sequence using only two integer indices as extra bookkeeping, with no auxiliary stack or buffer needed beyond the array that must hold the output anyway.
Time: O(n)
Space: O(1)
Approach 3
Build the reversed string by recursively computing reverse(s[1:]) and appending s[0] after it, or equivalently by repeatedly prepending characters onto a growing result string. This is correct but wasteful, since every concatenation with an immutable string copies the entire partial result built so far, so the total work across all characters grows quadratically.
Time: O(n^2)
Space: O(n)
Step by step
1. Point one finger at the very first character, another at the very last.
2. Swap the two characters your fingers are pointing at.
3. Move the first finger one step right, the second one step left, and repeat until they meet or cross.
Complexity
Time: O(n) - roughly n/2 swaps, one pass through half the string.
Space: O(1) extra space for the in-place pointer technique (though building a new reversed string, if required by an immutable string type, costs O(n)).
Edge cases
A single-character string (unchanged after reversal); a string up to 10^5 characters for performance.
Interview tip
This is usually a 30-second warm-up question - the real test is whether you can explain the two-pointer technique cleanly, since it reappears everywhere (palindrome checks, array reversal, and more).