BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Tokenization and NLP23 of 38 in topic

367. Map raw bytes to printable characters for byte-level BPE

MediumTokenization and NLP
Asked atMistralOpenAI

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.

Functiondef 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.

Worked example

"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
Tokenization and NLP
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.

Similar questions
  • Do one merge step of a BPE tokenizerMedium
  • Compute Levenshtein edit distanceMedium
  • Tokenize words with WordPiece greedy longest matchMedium
  • Build a vocabulary with special tokens, a minimum frequency and UNKMedium
All Tokenization and NLP problems
Report
367 / 903
Python 3solution.pyPython idle
1
2
3
4
def byte_level_encode(text):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS