Reverse a String Without Using Built-in Methods
Reverse a string in place on a char array without using library reverse helpers.
Convert the string to a char array, then swap the characters at a `left` and `right` pointer while moving them toward the center. When they meet, the array is reversed; build a new String from it. This runs in O(n) time and O(n) space for the array, without any reverse helper.
Problem Statement
Given a string, return it with its characters in reverse order. You must not use
StringBuilder.reverse() or any other library method that reverses for you — only
basic character access and swapping.
For example, "hello" becomes "olleh".
Input: A string s.
Output: A new string with the characters of s in reverse order.
Examples
Input: "hello"
Output: "olleh"h and o swap, e and l swap, and the middle l stays put.
Input: "Java"
Output: "avaJ"J and a swap, a and v swap, giving avaJ.
Constraints
0 <= s.length() <= 10^6No StringBuilder.reverse or Collections.reverse
Think Before You Code
Reveal the questions to ask yourself first
- Strings are immutable in Java, so what mutable structure will you work on?
- Which two positions should you swap first, and how do they move?
- When should the swapping stop so you do not undo your own work?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Two pointers swapping toward the middle.
- Convert
sto a char arraychars. - Set
left = 0andright = chars.length - 1. - While
left < right: swapchars[left]andchars[right], then incrementleftand decrementright. - Build and return
new String(chars).
Stopping when the pointers meet means each pair is swapped exactly once; swapping past the middle would reverse the reversal.
Dry Run
Walk through the example step by step
Reversing "hello" (chars = h e l l o):
left | right | swap | array
-----+-------+-------------+----------
0 | 4 | h <-> o | o e l l h
1 | 3 | e <-> l | o l l e h
2 | 2 | left<right? | stop (middle l stays)
Result: "olleh".
Solution
Reveal the full Java solution
public class ReverseString {
public static String reverse(String s) {
char[] chars = s.toCharArray();
int left = 0;
int right = chars.length - 1;
while (left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return new String(chars);
}
public static void main(String[] args) {
System.out.println(reverse("hello")); // olleh
System.out.println(reverse("Java")); // avaJ
}
}
Because Java strings are immutable, we copy the characters into a mutable array,
reverse that array with a symmetric two-pointer swap, and construct a fresh string
from the result. The loop stops when left meets or passes right, so a string of
odd length leaves its middle character untouched, which is correct.
O(n)Space: O(n) for the char arrayCommon Mistakes
- Trying to assign to `s.charAt(i)`; strings are immutable, so you must work on a char array.
- Looping `left` all the way to the end, which swaps every pair twice and restores the original.
Edge Cases to Test
- An empty string reverses to an empty string.
- A single-character string is unchanged.
- A palindrome like "level" reverses to itself.
Interview Follow-Ups
- How would you reverse the string using recursion instead of a loop?
- How would you reverse only the letters while keeping other characters in place?
- How does reversing a string relate to checking whether it is a palindrome?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →