Why interviewers love this: it looks simple, but it quietly checks whether you know that Java strings are immutable and how to build a new result safely.
Question: Reverse a String.
Answer: In Java, the easiest way is to create a new reversed string because String cannot be changed in place. A common solution is new StringBuilder(s).reverse().toString(), or a manual two-pointer swap if you want to show the algorithm. The main thing to remember is that reversing is O(n) time because you must look at each character at least once.
Interview-Ready Answer: I would say: In Java, I cannot modify a String directly because it is immutable, so I build a new reversed value. The simplest approach is StringBuilder.reverse(), which runs in linear time, O(n), and uses extra space for the new result. If the interviewer cares about Unicode edge cases, I would mention that reversing by raw char can be risky for emoji, so code points are safer.
Reversing a string means reading the text from the last unit to the first and producing a new string. In Java, this matters because String is immutable, which means its contents cannot be changed after creation. So every correct solution must create a new object or work through a mutable helper such as a char[] or StringBuilder.
String.That is the whole idea: two ends moving toward the center, like closing a zipper in reverse.
StringBuilder is the usual interview answerStringBuilder is a mutable sequence of characters. Mutable means it can change after creation. Because it can grow and change efficiently, it is much better than repeated + concatenation inside a loop. In interviews, new StringBuilder(s).reverse().toString() is short, correct, and easy to explain.
If the interviewer asks for the algorithm, use a char[] and swap the left and right elements. That shows you understand the mechanics instead of only knowing a library method. It is also a good way to explain complexity and two-pointer thinking.
Java char is a UTF-16 code unit, not always a full user-visible character. That matters for emoji and some historic scripts, because one visible symbol can be stored as two chars, called a surrogate pair. If you reverse raw char values, you can break those symbols. A safer general approach is to reverse by Unicode code points, which are full Unicode numbers representing characters.
| Approach | Good for | Trade-off |
|---|---|---|
StringBuilder.reverse() | Fast interviews | Shortest code |
char[] swap | Algorithm questions | More manual work |
| Code points | Unicode safety | Longer code |
All sensible solutions are O(n) time because every character or code point must be processed. Space is usually O(n) too because you build a new result. For a 1,000,000-character string, a plain char[] solution needs about 2 MB for the characters alone, and a code-point array can use more because each int is 4 bytes. That is why you would not use recursion for this problem: it adds unnecessary call-stack overhead and can blow the stack on long strings.
null: define a contract, usually by throwing IllegalArgumentException or returning null if your team prefers that style.char logic may break them, so mention code points if the interviewer cares about real Unicode.Memory hook: think of a train with cars numbered from both ends; you walk inward, swapping the first and last car, then the second and second-to-last, until the train faces the other way.
Real-World Example: In a multilingual checkout service, a small utility reversed order-reference text before sending it to a legacy partner API that expected an old mirrored format. The code worked in tests with English names, but in production a customer in Japan placed an order with an emoji in the display name. A developer had reversed raw char values, which split a surrogate pair and turned the emoji into �. The partner API rejected the record with a 400, retries piled up, and the support team saw repeated log lines like invalid utf-16 sequence and payload validation failed.
The fix was simple but important: stop treating every Java char as a whole character. The team moved to a code-point-based reverse for the legacy path, added tests with emoji and accented text, and documented the contract for null inputs. The lesson is that string reversal is not just a toy exercise; in real systems, text can contain more than plain ASCII, and the wrong reversal strategy can corrupt user data or break integrations.
public class Main {
// Simple interview solution: Java String is immutable, so we return a new String.
public static String reverseString(String s) {
if (s == null) {
throw new IllegalArgumentException("Input string cannot be null");
}
return new StringBuilder(s).reverse().toString();
}
// Unicode-safe reversal by code point.
// This keeps emoji and other supplementary characters intact.
public static String reverseByCodePoint(String s) {
if (s == null) {
throw new IllegalArgumentException("Input string cannot be null");
}
int[] codePoints = s.codePoints().toArray();
StringBuilder out = new StringBuilder(s.length());
for (int i = codePoints.length - 1; i >= 0; i--) {
out.appendCodePoint(codePoints[i]);
}
return out.toString();
}
public static void main(String[] args) {
String[] tests = { "hello", "", "A\uD83D\uDE0AB", "racecar" };
for (String test : tests) {
System.out.println("Original : [" + test + "]");
System.out.println("Reversed : [" + reverseString(test) + "]");
System.out.println("By code pt : [" + reverseByCodePoint(test) + "]");
System.out.println();
}
// Failure path: decide how your API should behave for null.
try {
System.out.println(reverseString(null));
} catch (IllegalArgumentException ex) {
System.out.println("Null case : " + ex.getMessage());
}
}
}Follow-up & Tricky Questions:
O(n) because you must touch every unit of text. The space is usually O(n) because the reversed result must be stored somewhere new.StringBuilder instead of StringBuffer? StringBuilder is faster in single-threaded code because it is not synchronized. StringBuffer is synchronized, meaning it adds thread-safety locks, which you usually do not need here.char[] in place? Use two pointers, swap the left and right elements, and move inward until they cross. That is the classic algorithmic version interviewers like to see on a whiteboard.char values. That prevents splitting surrogate pairs and is safer for emoji and other supplementary characters.StringBuilder.reverse() handle emoji correctly? It is better than naïve char swapping, but if you need user-perceived characters in all languages, code points are the safer answer. For many interview settings, saying this shows strong awareness of Unicode.char the same as a character? No. In Java, char is a UTF-16 code unit, which is sometimes only half of a real character.Common Mistakes:
+ inside a loop. Fix: use StringBuilder or a character array, because repeated concatenation creates many temporary strings.String is immutable. Fix: always return a new string rather than trying to edit the original.char logic can break emoji and code points are safer when needed.O(1) space. Fix: the reversed result itself takes space, so the usual answer is O(n) extra space.Memory Hook: Picture a train of cars: one hand starts at the front, one at the back, and they swap cars until the whole train points the other way.
Cheat Sheet:
String is immutable, so build a new result.new StringBuilder(s).reverse().toString().char[] with two pointers.O(n) time, usually O(n) space.chars.Practice Tasks:
char[] solution without using StringBuilder.reverse().