debug a problem involving String Iteration
Fast recall
Quick revision
Core answer
Practical understanding of String Iteration.
Repeating a definition without saying when to use it.
Give one trade-off and say how you would test it.
Interview-ready guide
Interview answer
The rune-safe reversal:
func reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
reverse("hello") // "olleh"
reverse("héllo") // "olléh" — é moves as ONE unitThe byte version and why it's wrong for Unicode:
b := []byte(s)
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
// "héllo" → corrupted — é's two bytes (195 169) become (169 195)
// — an invalid rune that decodes to U+FFFD garbageThe mechanics of the corruption: é is stored as two bytes; byte-reversal swaps the halves independently — they end up adjacent but in the wrong order, which is no longer é's encoding. The output string holds invalid UTF-8.
The costs of the safe version: []rune decode (O(n) + allocation), the swap (O(n/2)), the re-encode (O(n) + allocation). Three passes — the honest price of character-level correctness. The byte version is two passes and allocation-free — and only correct on KNOWN-ASCII input, a guarantee real text doesn't make.
The edge worth one sentence: rune reversal reverses code points, not graphemes — "é" as e + combining accent stays two runes and the combining accent ends up attached to the wrong character. True grapheme-safe reversal needs a segmenter (x/text or uniseg) — the rune version is the standard, correct-for-most answer.
debug a problem involving String Iteration
Direct Answer
Convert to []rune(s), swap ends inward, convert back: for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 { r[i], r[j] = r[j], r[i] }. Reversing BYTES destroys multibyte runes — the rune conversion is mandatory for Unicode-safe reversal.
Deep Dive & Architecture
The three-unit hierarchy, resurfacing one final time: bytes (storage — what's actually in the string), runes (code points — what []rune gives you), graphemes (user characters — what rune reversal still doesn't give you). The reversal question is a proxy for 'which unit does your string operation work on' — and every string function answer in this whole module has been the same question with a different function.
The corruption anatomy, byte by byte, for the definitive demonstration: "héllo" = [104 195 169 108 108 111]. Byte-reversed: [111 108 108 169 195 104] — the é pair (195 169) became (169 195): the continuation byte (10xxxxxx) now LEADS, which no valid sequence does. A decoder sees 169 (a continuation without a lead), yields U+FFFD, then 195 leads a 2-byte sequence that consumes 104 ('h') as its continuation — also invalid. Two runes destroyed by one swap. That's not abstract: this exact bug pattern appears in every language with UTF-8 + byte-indexed reversal, and Go's answer is the same as its iteration story — the rune is the character unit; say you mean it.
The allocation-conscious variant for interviews (the memory-sensitive follow-up): if reversal is in a hot path, the []rune allocation per call is the cost — the manual alternative is walking with utf8.DecodeRuneInString from both ends simultaneously (forward decoder + backward decoder — UTF-8's backward self-synchronization requires care: DecodeLastRuneInString), swapping into a pre-allocated []byte. Zero rune-slice allocation, byte-level buffer, rune-level semantics — the pattern the stdlib uses internally for its own bidi needs. The readable version remains []rune + swap; the optimized version is a niche answered only when profiling demands it.
Common Mistakes & Pitfalls
Repeating a definition without saying when to use it.
Check your understanding
Answer these without looking back at the article.
- What is String Iteration, and when would you use it?
- When would you use String Iteration in a real project?
- How would you compare String Iteration with an alternative?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.