Quick revision
The short answer
StringBuilder.reverse() reverses the order of chars but keeps each surrogate pair together, so a single emoji such as U+1F600 survives. What it cannot handle is a grapheme cluster, a symbol a person sees as one character but which is several code points: a letter followed by a combining accent, a flag made of two regional indicators, or an emoji with a skin-tone modifier or zero-width joiners. To reverse text correctly we split it into grapheme clusters with BreakIterator.getCharacterInstance() or the regex \X, and reverse the order of the clusters.
What to remember
In this answer, we saw that StringBuilder.reverse() keeps surrogate pairs together, so single emoji survive, but it knows nothing about grapheme clusters. Combining accents, flags made of two regional indicators, skin-tone modifiers and zero-width joiner sequences are reordered and change meaning. Reversing by cluster with BreakIterator or the regex \X is the correct approach, NFC normalization helps only for accented letters that have a composed form, and the more common real problem, truncating text to a visible length, needs the same cluster-aware tools.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
StringBuilder.reverse() reverses the order of chars but keeps each surrogate pair together, so a single emoji such as U+1F600 survives. What it cannot handle is a grapheme cluster, a symbol a person sees as one character but which is several code points: a letter followed by a combining accent, a flag made of two regional indicators, or an emoji with a skin-tone modifier or zero-width joiners. To reverse text correctly we split it into grapheme clusters with BreakIterator.getCharacterInstance() or the regex \X, and reverse the order of the clusters.
Why this question is asked
Text bugs like this one reach production because tests use names like "Alice" while real users write "Zoe" with an accent, add a flag to their profile or pick a skin tone for their emoji. Interviewers ask it to see whether we know the three levels of a Java string, chars, code points and grapheme clusters, and which API works at which level. In this answer, we will recap those three levels, see exactly what reverse() does, reproduce the broken cases, reverse by grapheme cluster, look at where normalization helps, and then ask whether reversing is the right feature at all, compared with the far more common problem of truncating text safely.
1A Quick Recap of chars, Code Points and Grapheme Clusters
A Java String is a sequence of UTF-16 code units called chars. A code point is the number Unicode gives to a character; code points up to U+FFFF fit in one char, and those above it, which include most emoji, take two chars called a surrogate pair. A surrogate pair is meaningless if its halves are separated or swapped.
A grapheme cluster is what a reader thinks of as one character. Often it is one code point, but not always. The letter e followed by U+0301, a combining acute accent, displays as one accented e. A flag is two regional indicator symbols: Canada is the indicators C and A. A thumbs-up with a medium skin tone is the thumbs-up code point followed by a modifier, U+1F3FD. A family emoji is several people joined by U+200D, the zero-width joiner. In each case the order of the parts carries the meaning.
2What reverse() Actually Does
The Javadoc of StringBuilder.reverse() promises one thing about Unicode: surrogate pairs are treated as single units. It reverses the chars and then swaps back any pair whose halves ended up in the wrong order. So a string with ordinary emoji reverses correctly.
| Text | chars | code points | grapheme clusters |
|---|---|---|---|
| a | 1 | 1 | 1 |
| emoji U+1F600 | 2 (surrogate pair) | 1 | 1 |
| e + U+0301 accent | 2 | 2 | 1 |
| flag of Canada | 4 | 2 regional indicators | 1 |
| thumbs-up + skin tone | 4 | 2 | 1 |
| family of three | 8 | 5 (3 people + 2 joiners) | 1 |
reverse() works at the code point level, so it is only correct for rows where code points and clusters agree.Notice that none of these results throws an exception or produces invalid UTF-16. Each output is a perfectly legal string; it just means something else. That is why the bug passes tests and reaches users: the code does exactly what it promises, at the level of code points, while the user thinks in grapheme clusters.
The family emoji fails in the same way. Reversing man, joiner, woman, joiner, girl gives girl, joiner, woman, joiner, man, which most fonts draw as a different family or as three separate people. Any sequence whose meaning depends on order breaks.
3Reversing by Grapheme Cluster
The correct approach splits the text into grapheme clusters, reverses the list of clusters, and joins them. Each cluster keeps its inner order, so accents stay after their letters and flags keep their two indicators in order.
BreakIterator.getCharacterInstance() returns an iterator over user-perceived characters, and the regex \X matches one extended grapheme cluster. Both follow the Unicode rules for cluster boundaries, which the JDK updates as Unicode adds emoji. Older JDKs handled combining marks well but split some emoji sequences, and the JDK moved BreakIterator to the full extended grapheme cluster rules in recent releases (around JDK 20), so check your version and test with a flag, a skin tone and a family emoji.
So the cost of correctness is one pass to find boundaries and a list of small strings. For display names of a few dozen characters that is negligible.
4Where Normalization Helps and Where It Does Not
Unicode often allows two spellings of the same text. The accented e can be one code point, U+00E9, or e followed by U+0301. Normalizer.normalize(s, Normalizer.Form.NFC) converts to the composed form wherever a composed character exists.
That makes reverse() happen to work for many European names. But it is not a fix. Many combinations have no composed form, for example a letter with two stacked marks in some languages, and emoji sequences such as flags, skin tones and families are never composed into one code point. NFC is still worth applying to stored names, because it makes equality and search behave consistently, but correctness for reversal needs grapheme clusters.
5Is Reversing the Right Feature at All?
A senior answer also questions the requirement. Reversing user text is rare in real products; it mostly appears in puzzles. The problems that actually reach production are the related ones: limiting a name to 50 characters, truncating a preview with an ellipsis, counting characters for a form, or storing text in a column with a length limit. Each of them breaks in the same way when the code cuts by char index.
For example, s.substring(0, 50) can cut a surrogate pair in half and leave an unpaired surrogate, which some JSON encoders and databases reject or replace with U+FFFD, the replacement character. Cutting by code point is safer but can still split a flag or drop a skin-tone modifier. So the same BreakIterator approach is the right tool: walk 50 cluster boundaries and cut there. We should also check what the storage layer counts, because a VARCHAR(50) in one database counts characters, while in others the limit is in bytes, where one emoji takes four bytes in UTF-8.
6Follow-up questions
How do you truncate a string to 50 user-visible characters safely?
I walk grapheme boundaries with BreakIterator.getCharacterInstance(): call first(), then next() up to 50 times, and if I reach DONE the string is already short enough. Otherwise I return s.substring(0, boundary), which never splits a surrogate pair, a flag or an accent from its letter. If the limit comes from a database column, I check whether the column counts characters or bytes and enforce the same unit, and I normalize to NFC first so the count is stable.
Does String.length() ever equal the number of characters a user sees?
Only for text where every visible character is one code point in the Basic Multilingual Plane. length() counts UTF-16 chars, codePointCount counts code points, and only a grapheme cluster count, from BreakIterator or counting \X matches, matches what a person sees. For a form that says maximum 30 characters, the cluster count is the honest one.
Is StringBuilder.reverse() broken?
No, it does exactly what its contract says: it reverses code points and keeps surrogate pairs together. The bug is in using a code point operation for a grapheme cluster requirement. That is why I would fix the calling code, not look for a different builder method.
7Conclusion
In this answer, we saw that StringBuilder.reverse() keeps surrogate pairs together, so single emoji survive, but it knows nothing about grapheme clusters. Combining accents, flags made of two regional indicators, skin-tone modifiers and zero-width joiner sequences are reordered and change meaning. Reversing by cluster with BreakIterator or the regex \X is the correct approach, NFC normalization helps only for accented letters that have a composed form, and the more common real problem, truncating text to a visible length, needs the same cluster-aware tools.
Open with
StringBuilder.reverse works at the code point level. It reverses the chars and then repairs surrogate pairs, so a single emoji like a smiley comes through intact. What it does not understand is grapheme clusters, the things a user sees as one character but that are several code points.
Then, if they want more
An e followed by a combining accent comes out with the accent first, attached to nothing. A flag is two regional indicators, so Canada, C and A, reversed becomes A and C, which is the flag of Ascension Island.
A thumbs-up with a skin tone becomes a lone colour swatch and a yellow thumb, and a family emoji joined with zero-width joiners turns into a different family. None of these throws, which is why tests pass.
To do it correctly I split the string into grapheme clusters with BreakIterator.getCharacterInstance or the regex backslash X, reverse the list of clusters and join them, so each cluster keeps its inner order. I would check the JDK version, because full emoji cluster rules in BreakIterator arrived in recent releases.
Normalizing to NFC makes many accented letters a single code point, but it never composes emoji sequences, so it is not a fix on its own. And I would ask whether we really need to reverse names.
The common production problem is truncation, and the same cluster-aware approach is what I would use to cut a name to 50 visible characters.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 20
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A method reverses user-entered display names with new...": https://scrivio-interview-library.netlify.app/java/coding-problems/ecd581e743/