ORIGINAL TECHNICAL PRACTICE
Define what a character means
An interviewer asks you to reverse a string. What assumptions about Unicode should you clarify before writing a loop?
Written for independent practice. The model answer presents one approach; clarify constraints and explain alternatives.
A REASONED APPROACH
Model answer.
A string’s storage units are not always the characters a person sees. Clarify whether the task concerns code units, Unicode code points or grapheme clusters, and choose an iteration method matching that requirement.
Work through it
- Use an ASCII example first, then test an emoji represented by a surrogate pair and a letter combined with a diacritic.
- In JavaScript, spreading a string iterates code points, avoiding the common surrogate-pair split, but still does not preserve every grapheme cluster.
- For user-perceived characters, use a Unicode grapheme segmentation facility and reverse the resulting segments. Decide separately whether normalization is part of the input contract.
Tradeoffs to discuss
- More complete Unicode handling can need platform support or additional segmentation work. A restricted ASCII task may reasonably choose a much simpler implementation.
Common pitfalls
- Splitting on an empty string can break surrogate pairs. Treating every code point as a complete displayed character still breaks some emoji sequences and combining marks.