Reverse String in Java | Fresher Interview Question
Fast recall
Quick revision
Core answer
char[] and swap with two pointers - one at the start, one at the end - moving inward until they meet. This is O(n) time and O(n) space for the char array (or O(1) extra if you reverse the array in place). Strings are immutable in Java, so you build a new string from the reversed characters.Whether you can reverse manually with two pointers and remember Java strings are immutable
Trying to mutate the String directly (impossible - it's immutable) or only reversing half/over-swapping past the middle
Use the two-pointer swap, stop at the middle, and mention immutability forces a char[] or StringBuilder
Interview-ready guide
Interview answer
The first thing I'd point out is that Java strings are immutable, so I can't just swap characters inside the string itself.
I convert the string to a char array, which is mutable, do the work there, and build a new string at the end. The reversal itself is a clean two-pointer swap. I put one index at the start, i equals zero, and another at the end, j equals length minus one.
I swap the characters at i and j, then move i forward and j backward, and I keep going while i is less than j. The moment they meet in the middle I stop. That middle stop matters: if I kept going all the way to the end, I'd swap every pair twice and end up with the original string again.
This runs in O of n time because I touch each character exactly once, and the char array is O of n space.
A common mistake is trying to mutate the string directly, which doesn't even compile. Another is building the reversed string by concatenating in a loop with plus equals, which is O of n squared because strings are immutable and each concatenation copies everything.
If I just need it done in real code, I'd use new StringBuilder of s dot reverse dot toString, which is one line, but in an interview the expectation is usually that I show the two-pointer loop.
One subtlety I'd mention if asked is that characters outside the basic range, like emoji, are stored as surrogate pairs, and a naive per-character reversal can split them, so true Unicode-safe reversal needs extra handling.
Reverse String in Java | Fresher Interview Question
Direct Answer
Convert the string to a char[] and swap with two pointers - one at the start, one at the end - moving inward until they meet. This is O(n) time and O(n) space for the char array (or O(1) extra if you reverse the array in place). Strings are immutable in Java, so you build a new string from the reversed characters.
Deep Dive & Architecture
A Java String is immutable, so its characters cannot be swapped in place. A simple manual solution first calls toCharArray(), swaps matching positions, and builds a new String from the changed array.
Worked example: java
Put left at index 0 and right at index 3. Swap j and a, giving aavj. Move both pointers inward and swap the middle a and v, giving avaj. The pointers have now crossed, so the reverse is complete.
The invariant is that everything outside [left, right] is already in its final position. Each swap settles two characters, which is why the loop only needs to reach the center. A one-character or empty string needs no swap.
The loop is O(n) and the character array uses O(n) space. That extra array is unavoidable when the input is a String, because the original object cannot be modified. If the method receives a mutable char[], the same two-pointer loop uses O(1) extra space.
For interview-sized ASCII examples this is enough. For full Unicode text, one visible symbol may use more than one UTF-16 char; reversing code points is safer when emoji or supplementary characters must remain intact.
Code Example
String reverse(String s) {
char[] c = s.toCharArray();
int i = 0, j = c.length - 1;
while (i < j) {
char tmp = c[i]; // swap ends
c[i] = c[j];
c[j] = tmp;
i++; j--; // move inward
}
return new String(c);
}
// "java" -> c = [j,a,v,a]
// i=0,j=3: swap -> [a,a,v,j]; i=1,j=2: swap -> [a,v,a,j]; i=2,j=1 stop
// result: "avaj"
Without the manual rule, new StringBuilder(s).reverse().toString() does the same in one line - but interviewers usually want the loop.
Common Mistakes & Pitfalls
- Mutating the String:
s.charAt(i) = xdoesn't compile - strings are immutable; use achar[]. - Swapping past the middle: looping while
i <= j(or to the end) re-swaps and un-reverses. - Concatenation in a loop: building the reversed string with
result += cis O(n^2); use a char array or StringBuilder. - Unicode: surrogate pairs (emoji) need care - per-
charreversal can split them.
Check your understanding
Answer these without looking back at the article.
- What is the time complexity of common array operations in Java?
- How do you find duplicates in an array in Java?
- How do you check if two strings are anagrams of each other?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.