Detailed explanation
The simplest way to reverse a String in Java is to create a StringBuilder, call reverse(), and convert the result back to a String: String s = "hello"; String reversed = new StringBuilder(s).reverse().toString(); The result is "olleh". String is immutable, so you cannot modify its characters in place. This expression leaves s unchanged and creates a reversed value. StringBuilder is useful here because it provides a mutable character sequence with a built-in reversal operation.
Under the hood, StringBuilder reverses its internal sequence by exchanging elements from opposite ends, moving towards the centre. Its implementation also handles UTF-16 surrogate pairs so that a valid pair representing a supplementary Unicode character remains intact. For a string of length n, constructing the builder, reversing its contents, and producing the final String together take O(n) time and O(n) additional space. Although reversing an existing builder works within its storage, the complete expression is not an O(1)-space solution because it creates storage for the builder and result.
If an interviewer asks you not to use reverse(), you can traverse the input backwards and append each char to a builder: StringBuilder result = new StringBuilder(s.length()); for (int i = s.length() - 1; i >= 0; i--) { result.append(s.charAt(i)); } String reversed = result.toString(); Another approach is to call toCharArray(), swap the first and last characters using two pointers, move those pointers inward, and construct a String from the array. Both manual approaches take O(n) time and O(n) additional space, but naive char-based reversal can split surrogate pairs.
Interviewers usually look for a correct implementation, an explanation of immutability, and accurate complexity analysis. Mention that empty strings and single-character strings need no special reversal logic, while null requires an explicit contract: reject it or handle it deliberately. For ordinary coding exercises, StringBuilder.reverse() is the preferred concise answer. For user-visible Unicode text, even this method does not preserve every grapheme cluster, such as a letter followed by a combining accent; that requires grapheme-aware handling.
Key points to remember
- Use new StringBuilder(s).reverse().toString() for a concise solution.
- String is immutable; the original input remains unchanged.
- The complete solution takes O(n) time and O(n) additional space.
- Without reverse(), traverse backwards or swap elements in a char array.
- StringBuilder preserves valid surrogate pairs, but not all grapheme clusters.
Common mistakes candidates make
- Trying to call reverse() directly on a String.
- Repeatedly concatenating immutable Strings in a loop, causing O(n²) copying overall.
- Assuming each char represents a complete Unicode character and that manual char reversal is always safe.
Likely follow-up questions
Can you reverse a String in place?
No, a String is immutable. You can reverse a mutable char array or StringBuilder in its own storage, but converting a String into either representation requires additional storage.
Why use StringBuilder instead of StringBuffer?
StringBuffer provides synchronized methods, whereas StringBuilder does not. For a local reversal operation where the builder is not shared across threads, StringBuilder avoids unnecessary synchronization.
What happens if the input is null?
Passing a null String to the StringBuilder constructor throws NullPointerException. Decide whether your method should reject null or return a defined value, and implement that behaviour explicitly.
Interview tip: Start with the one-line StringBuilder solution, explain immutability and complexity, then offer a manual approach if asked.
