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

357. Do one merge step of a BPE tokenizer

MediumTokenization and NLP
Asked atAnthropicMistralOpenAI

Byte Pair Encoding (BPE) builds a tokenizer by repeatedly finding the most common pair of neighbouring symbols and gluing it into one new symbol. Run enough merges and frequent chunks like "ing" or "the" become single tokens.

Write bpe_merge_step(words) where words is a list of lists of symbols, for example [["l","o","w"], ["l","o","w","e","r"]].

1. Count every adjacent pair across all words.
2. Pick the most frequent pair. On a tie, pick the pair that comes first in alphabetical order of (left, right).
3. Return (pair, new_words), where new_words has every occurrence of that pair merged into one symbol (left + right), scanning each word left to right without overlap.

If no pair exists, return (None, words).

Functiondef bpe_merge_step(words)

Example 1

Input: words = [["l","o","w"],["l","o","w","e","r"]]

Output: (("l","o"), [["lo","w"],["lo","w","e","r"]])

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

Count pairs with zip(w, w[1:]).

Hint 2

Pick the max count, break ties with min() over the tied pairs, then rebuild each word left to right.

Understand the idea

Byte Pair Encoding learns a vocabulary by noticing which two neighbouring symbols appear together most often and fusing them into one new symbol. It is like noticing you always write 't' then 'h' and inventing a single key for 'th'. One merge step has two parts: count every adjacent pair across all words, then rewrite the words with the winning pair joined.

Worked example

words [a, b, c] and [a, b, d]. Pairs: (a, b) appears 2 times, (b, c) once, (b, d) once. The winner is (a, b), so the new words are [ab, c] and [ab, d]. If two pairs tie on count, the alphabetically smaller (left, right) pair wins. While rewriting, scan left to right and, after a merge, jump past both symbols, so [a, a, a] with pair (a, a) becomes [aa, a], not two overlapping merges.

best pair = highest count, then smallest pair; merge without overlap

GPT style tokenizers are trained by repeating this step thousands of times.

Topics
Tokenization and NLP
Companies3
  • Anthropic a candidate reported this question Coding Exercises & ML System Design: Real AI Interview Questions (landedjobs)
  • 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
  • Compute Levenshtein edit distanceMedium
  • Tokenize words with WordPiece greedy longest matchMedium
  • Build a vocabulary with special tokens, a minimum frequency and UNKMedium
  • Rank documents for a query with smoothed TF-IDF and cosine similarityMedium
All Tokenization and NLP problems
Report
357 / 903
Python 3solution.pyPython idle
1
2
3
4
def bpe_merge_step(words):
# 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