Unicode String Length
Problem
A profile service validates display names before writing them to a downstream store, and produces a short preview for list views:
MAX_NAME = 50 # the profile store rejects names longer than 50
def validate_display_name(name: str) -> str:
name = name.strip()
if len(name) > MAX_NAME:
raise ValueError("display name too long")
return name
def preview(name: str, width: int = 12) -> str:
return name[:width] + "…" if len(name) > width else nameThree bug reports arrived in the same week:
- Some names pass validation and are then rejected by the store with "value exceeds 50 bytes". All of them contain non-Latin characters or emoji.
previewsometimes renders a broken glyph or a stray box at the cut point.- Two users report that a duplicate-name check says "José" is available even though a user called "José" already exists.
Explain all three with one underlying cause and fix the code.
Examples
"Zoë"typed on one keyboard is"Zoë"(3 code points, 4 UTF-8 bytes); the same name pasted from another app can be"Zoë"(4 code points, 5 bytes):efollowed by a combining diaeresis. They print identically,lendiffers, and==isFalse."👨👩👧"(family emoji) haslen == 5(three people joined by two zero-width joiners) and is 18 UTF-8 bytes. A name of 10 such emoji haslen == 50, passes the check, and is 180 UTF-8 bytes.preview("Ana 🇧🇷 Silva", 5)→"Ana 🇧"then"…". The flag is two regional-indicator code points; cutting after the first leaves a lone indicator that renders as a boxed letter.
Hints
len(s)counts code points. Name three other units a "length" could be measured in, and decide which one each bug report is about.- Compare
"é".encode()for the precomposed and decomposed spellings. Then look atunicodedata.normalize. - A safe byte-truncation must never cut in the middle of a multi-byte sequence. What does
bytes.decode(errors="ignore")do with a trailing partial sequence?
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.