Tokenization and NLP practice problems
29 problems · 0 solved
Before a language model sees text, a tokenizer cuts it into pieces and maps each piece to an id. The choice of tokenizer affects cost, speed and how well a model handles languages like Hindi, Tamil or Kashmiri. It is similar to deciding how to break words into syllables before teaching someone to read. These problems cover modern tokenizers and classic NLP basics. You will pre-tokenize text into words, numbers and symbols, extract character n-grams, encode text with a byte fallback, decode SentencePiece pieces, do one merge step of a BPE tokenizer and encode a word with ranked merges, tokenize with WordPiece greedy longest match, map tokens back to character offsets, find the best segmentation with a unigram model and Viterbi, score words with TF-IDF, compute Levenshtein edit distance, train a bigram language model and measure perplexity, and score bigrams with Kneser-Ney smoothing. Start with the Easy problems on pre-tokenization, n-grams and TF-IDF. Then do BPE, WordPiece and edit distance, and finish with unigram segmentation and the smoothed language models. Tokenizer bugs cause strange model behaviour, and interviewers often ask how BPE works.
Easy 12
- Score words by TF-IDF
- Encode text with a byte fallback so no character is ever unknown
- Normalize text by lowercasing, stripping accents and collapsing spaces
- Count word n-grams and return the k most frequent with stable ties
- Build a bag-of-words count matrix over a fixed vocabulary
- Turn WordPiece tokens back into text by merging ## pieces
- Pre-tokenize text into words, numbers and symbols
- Extract character n-grams with boundary markers
- Decode SentencePiece pieces back to text
- Build next-token training pairs with a sliding window and stride
- Measure tokenizer compression and fertility on a text sample
- Join word tokens back into readable text
Medium 11
- Do one merge step of a BPE tokenizer
- Compute Levenshtein edit distance
- Tokenize words with WordPiece greedy longest match
- Build a vocabulary with special tokens, a minimum frequency and UNK
- Rank documents for a query with smoothed TF-IDF and cosine similarity
- Clean text with stopword removal and a rule-based suffix stemmer
- Truncate a sentence pair to a token budget with the longest-first strategy
- Encode a word with ranked BPE merges
- Map tokens back to character offsets
- Split text so special tokens stay whole
- Map raw bytes to printable characters for byte-level BPE
Hard 6
- Train a bigram language model and measure perplexityPro
- Find the most likely segmentation with a unigram model and ViterbiPro
- Encode a batch with CLS and SEP, truncation, padding side and an attention maskPro
- Recover the edit operations behind a Levenshtein distance by backtrackingPro
- Find collocations by ranking bigrams with pointwise mutual informationPro
- Score bigrams with interpolated Kneser-NeyPro