367. Map raw bytes to printable characters for byte-level BPE
Why it matters GPT-2 style tokenizers use this byte to character table, which is why their vocab files show a G-with-dot symbol at the start of words.
Byte-level BPE works on the 256 possible bytes, but merge tables are stored as text, so every byte is first mapped to a visible Unicode character. Spaces become a visible marker.
Write byte_level_encode(text). Build a table for bytes 0 to 255: bytes 33 to 126, 161 to 172 and 174 to 255 map to the character with the same code point. The remaining bytes, in increasing order, map to chr(256), chr(257), chr(258) and so on. Encode text as UTF-8 and return the string of mapped characters.
def byte_level_encode(text)Example 1
Input: byte_level_encode("hi there")
Output: "hi\u0120there" (the space, byte 32, is the 33rd remapped byte
Explanation: So it becomes chr(288)).
Example 2
Input: text = 'caf\u00e9'
Output: 'café'
Example 3
Input: len(byte_level_encode('\U0001F600'))
Output: 4
Constraints
- Python 3 standard library. Code runs in your browser.
- Your code must pass all 4 tests to be accepted.
Hint 1
Collect the 'keep' bytes in a set; walk 0..255 and give the others 256 + n with a counter n.
Hint 2
Build the keep set from three ranges.
Hint 3
Use a counter for the remapped bytes.
Hint 4
Remember text.encode('utf-8').
Common mistakes
- Missing one of the three printable ranges.
- Mapping characters instead of UTF-8 bytes.
Understand the idea
Byte-level BPE can tokenize any text because it starts from the 256 possible bytes, so nothing is ever unknown. The catch is that some bytes are invisible (space, newline, control codes), and merge files are plain text. The fix is a fixed table that gives every byte a visible stand-in character, like giving every guest a name badge.
Bytes that already print nicely (33 to 126, 161 to 172, 174 to 255) keep their own character. The other 68 bytes are numbered in order from 256. Byte 0 becomes chr(256), ..., byte 10 (newline) becomes chr(266), byte 32 (space) becomes chr(288), which looks like a capital G with a dot.
"hi there" has bytes h, i, 32, t, h, e, r, e, which map to "hi" + chr(288) + "there".
mapped = [table[b] for b in text.encode("utf-8")]GPT-2 and many later tokenizers use this table, so their vocab files show that dotted G at the start of tokens that begin with a space.
Shapes and data flow
text -> UTF-8 bytes (n,) -> n printable characters
Topics
Companies2
- Mistral the topic appears in interview reports Mistral AI Interview Guide 2026: European Frontier Lab, Open-Weight Strategy, MoE, and La Plateforme
- OpenAI a candidate reported this question Coding Exercises & ML System Design: Real AI Interview Questions (landedjobs)
From public interview reports and guides. Not affiliated with or endorsed by these companies.