LearnQuestion bank

In about 45 minutes, write a function that learns N BPE merges from a small corpus and an encode() that applies them to new text. Then discuss complexity and how you would make encoding fast.

Applied LLMCodingMedium

Answer it yourself first, out loud or on paper, then compare.

Answer

Model answer, written in a candidate's voice

In short

  • Pre-tokenise on whitespace and keep a word-frequency table so merges never cross word boundaries
  • Count overlapping pairs correctly, e.g. aaaa contributes three aa pairs, and apply merges left-to-right non-overlapping
  • Break ties deterministically, e.g. highest count then lexicographically smallest pair, and assign ranks in merge order
  • Encode by repeatedly applying the lowest-rank available merge; naive cost is about O(L^2) per word
  • Optimise with a linked list plus priority queue of pair ranks, lazy deletion, and per-word cache
  • Test empty input, Unicode/bytes, repeated characters, and unseen characters; use a byte-level base vocabulary for full coverage

In full

I would start by splitting text on whitespace, counting unique words, writing each word as a sequence of bytes (the same base vocabulary the encoder uses), and learning merges only inside words so boundaries are never crossed. For each iteration I count all adjacent pairs across the frequency-weighted corpus, including overlapping pairs such as aaaa contributing three aa pairs, choose the best pair with a deterministic tie-break such as highest count then lexicographically smallest pair, assign it the next rank, and rewrite every occurrence left-to-right non-overlapping. Encoding then starts from the byte-level base vocabulary, so any Unicode text or raw bytes are representable, and repeatedly finds the lowest-rank pair present in the current token sequence and merges it. The naive encoder is roughly O(L^2) per word because each merge scans the sequence, so for speed I would keep a linked list of tokens, local pair counts, a priority queue keyed by merge rank with lazy deletion, and a cache keyed by word or token sequence. I would test empty input, repeated characters, Unicode and arbitrary bytes, and unseen characters, and I would be explicit that a character-level vocabulary needs an unk fallback while a byte-level vocabulary avoids that failure mode.

Corrected by an independent AI reviewer.

What a strong answer covers

  • Pre-tokenises on whitespace and works on a word-frequency table so merges do not cross word boundaries
  • Correct pair counting and merge application, including overlapping pairs (“aaaa”), with deterministic tie-breaking
  • Encoding applies the lowest-rank available merge repeatedly; states naive cost and an optimisation (priority queue / linked list, caching per word)
  • Tests empty input, Unicode/bytes, repeated characters, unseen characters
  • Mentions a byte-level base vocabulary for full coverage

Common mistakes

  • Mishandling overlapping pairs
  • recounting the whole corpus after every merge without acknowledging the cost