---
title: In about 45 minutes, write a function that learns N BPE merges from a small corpus and an encode() that applies them to…
url: https://gurkanozkan.ai/learn/questions/applied-llm-fnd-02/
markdown: https://gurkanozkan.ai/learn/questions/applied-llm-fnd-02.md
kind: question
role: Applied LLM
stage: Coding
difficulty: Medium
json: https://gurkanozkan.ai/api/learn/questions/applied-llm-fnd-02.json
robots: noindex
---

# 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 LLM · Coding · Medium

## 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

## Topic guides

- [Coding for ML and LLM engineering](https://gurkanozkan.ai/learn/topics/coding-engineering.md)
---

*Study material compiled with AI: drafted by a local model (Inkling 2 Mini) from public sources, then checked against those sources by independent AI reviewers (Claude) in November 2026. Not reviewed line by line by a person.*
