#344 Reverse String
frequently reported atTCSBloombergAmazonMicrosoftGoogleApple
the problem
In plain words
Reverse a character array in place, using O(1) extra memory.
in s = ["h","e","l","l","o"]
out ["o","l","l","e","h"]
in s = ["H","a","n","n","a","h"]
out ["h","a","n","n","a","H"]
- 1 ≤ s.length ≤ 10⁵
- Printable ASCII characters
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternTwo pointers (opposite ends)Recursion on a smaller input
Reversing = swap the two ends, then reverse the middle. The middle is a smaller copy of the same problem — or just a loop with two pointers.
solutions
From brute force to optimal
Fill a new array from the back, then copy it over the original.
- copy[i] = s[n − 1 − i].
- Copy everything back into s.
Two linear passes.
The extra array — breaks the O(1) memory rule.
class Solution { public void reverseString(char[] s) { char[] copy = new char[s.length]; for (int i = 0; i < s.length; i++) copy[i] = s[s.length - 1 - i]; System.arraycopy(copy, 0, s, 0, s.length); // extra O(n) array → not "in place" }}Swap s[left] and s[right], then recurse on left + 1, right − 1.
- Base case: left ≥ right (0 or 1 characters left).
- Swap the ends.
- Recurse on the inner range.
n / 2 calls, each doing one swap.
n / 2 stack frames — still not O(1) memory, even though no array is allocated.
class Solution { public void reverseString(char[] s) { reverse(s, 0, s.length - 1); } private void reverse(char[] s, int left, int right) { if (left >= right) return; // 0 or 1 chars left: nothing to swap char tmp = s[left]; s[left] = s[right]; s[right] = tmp; reverse(s, left + 1, right - 1); // the inner part is a smaller copy of the problem }}The same swaps, but driven by a while loop instead of the call stack.
- left = 0, right = n − 1.
- While left < right: swap, left++, right--.
n / 2 swaps.
Two indices and one temp char.
class Solution { public void reverseString(char[] s) { int left = 0, right = s.length - 1; while (left < right) { char tmp = s[left]; s[left++] = s[right]; s[right--] = tmp; } }}in the room
How to say it in the interview
talk track
- Show the recursive version if the interviewer is testing recursion, then point out its hidden O(n) stack space.
- Convert it to the loop to meet the O(1) memory requirement.
traps
- Claiming the recursive version is O(1) space — the call stack counts.
- Base case
left == rightalone misses even lengths; useleft >= right.
what they ask next
Follow-ups
Reverse only the vowels (LC 345)?
Same two pointers, but each pointer skips non-vowels before swapping.
before you submit
Edge cases to test
- One character: nothing changes.
- Odd length: the middle character stays where it is.
- 10⁵ characters: a recursive version is 50,000 calls deep — on a small stack that's a
StackOverflowError, which is why the loop is the optimal answer.
practise next
Similar problems
- #125Valid PalindromeEasyin Two Pointers
- #206Reverse Linked ListEasyin Linked Lists