LLM & Transformer Fundamentals·Question 20 of 109Intermediate

Walk me through the BPE training algorithm step by step.

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Byte-Pair Encoding learns a vocabulary of subword merges from a corpus:

  1. Initialize the vocabulary with the base alphabet - in byte-level BPE (GPT-2 onward), all 256 byte values, which guarantees any string is representable. Represent the corpus as sequences of these base symbols.
  2. Count all adjacent symbol pairs across the corpus.
  3. Merge the most frequent pair into a new single symbol, add it to the vocabulary, and record the merge rule (e.g. t + h → th).
  4. Repeat steps 2-3 until the target vocabulary size is reached (e.g. ~50k merges for GPT-2's 50,257 vocab; ~128k for Llama 3's).
Full worked answer with diagram

Byte-Pair Encoding learns a vocabulary of subword merges from a corpus:

  1. Initialize the vocabulary with the base alphabet - in byte-level BPE (GPT-2 onward), all 256 byte values, which guarantees any string is representable. Represent the corpus as sequences of these base symbols.
  2. Count all adjacent symbol pairs across the corpus.
  3. Merge the most frequent pair into a new single symbol, add it to the vocabulary, and record the merge rule (e.g. t + h → th).
  4. Repeat steps 2-3 until the target vocabulary size is reached (e.g. ~50k merges for GPT-2's 50,257 vocab; ~128k for Llama 3's).

The output is an ordered list of merge rules. Encoding at inference time replays them: split the text into base symbols, then apply merges in the order they were learned wherever they occur, greedily, until no more apply. Encoding is deterministic; the learned merge order is the tokenizer.

from collections import Counter
def bpe_step(corpus):                     # corpus: list of (symbol_tuple, freq)
    pairs = Counter()
    for word, freq in corpus:
        for a, b in zip(word, word[1:]):
            pairs[(a, b)] += freq
    return max(pairs, key=pairs.get)      # pair to merge next

Practical details that signal real understanding: real tokenizers apply a pre-tokenization regex first (splitting on whitespace/category boundaries, so merges don't cross word boundaries - GPT-2's regex famously special-cases contractions like 's), and they handle leading spaces as part of tokens (" the" and "the" are different tokens). Frequency-based merging means the vocabulary mirrors the training corpus distribution - which is exactly why tokenizers trained on English-heavy data fragment other languages. Note also the contrast: WordPiece picks the merge maximising corpus likelihood rather than raw frequency, and unigram-LM (SentencePiece's other algorithm) works top-down - start with a large candidate vocab and prune tokens whose removal least hurts likelihood.

Worth sketching. The loop and its exit condition are the algorithm; everything else is bookkeeping.

flowchart LR
    I["vocab = 256 byte values"] --> PT["pre-tokenize by regex<br/>merges cannot cross word boundaries"]
    PT --> CNT["count all adjacent symbol pairs"]
    CNT --> MG["merge the most frequent pair,<br/>append the rule to the list"]
    MG --> D{"target vocab size reached?"}
    D -->|"no"| CNT
    D -->|"yes"| OUT["ordered merge list is the tokenizer"]
    OUT --> ENC["encoding replays the merges greedily"]

What a good answer covers

  • Count all adjacent symbol pairs across the corpus
  • The output is an ordered list of merge rules
  • Encoding at inference time replays them
  • Encoding is deterministic; the learned merge order is the tokenizer
  • Practical details that signal real understanding

Key terms: Initialize · Count · Merge · Repeat · Encoding · pre-tokenization · WordPiece · unigram-LM

Interviewer follow-ups to expect

  1. Why does pre-tokenization matter - what would go wrong without word-boundary constraints?

  2. Why do "the" and " the" need to be separate tokens?

  3. How does unigram-LM tokenization differ conceptually from BPE?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep