BPE Merge Step
One step of how every tokenizer vocabulary is built: find the most frequent adjacent symbol pair across the corpus and merge it everywhere. Run this a few thousand times and you have byte-pair encoding — the algorithm behind the tokens every model reads.
The problem
Given words — a list of words, each a list of symbols — perform one BPE training step: count every adjacent symbol pair across all words, find the most frequent pair (break ties by choosing the lexicographically smallest pair), and merge every non-overlapping occurrence of that pair, left to right, into a single concatenated symbol. Return a pair [best_pair, new_words] where best_pair is the merged pair and new_words the corpus after the merge.
words = [['l','o','w'], ['l','o','w','e','r']][('l','o'), [['lo','w'], ['lo','w','e','r']]]words = [['a','a','a']][('a','a'), [['aa','a']]]after many stepsfrequent substrings become single tokens- Ties on count → lexicographically smallest pair wins (compare as a 2-tuple / 2-array of strings).
- Merging is left-to-right and non-overlapping within a word.
- Words with fewer than 2 symbols contribute no pairs and pass through unchanged.
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.
Implement bpe_merge_step(words) → [best_pair, new_words]: count adjacent pairs, pick the most frequent (lexicographically smallest on ties), merge it everywhere left-to-right without overlaps.
- Count pairs with a dict keyed by the (left, right) tuple, scanning each word once.
- Pick the winner by sorting on (-count, pair) or an equivalent max with a tie-break key.
- Merge with an index walk: if symbols i and i+1 equal the pair, emit the concatenation and jump i by 2; else emit symbols[i] and jump by 1.
- Check the ['a','a','a'] case: exactly one merge should happen, producing ['aa','a'].
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.