Anthropic Screening Question: Longest-Match Tokenization with a Trie
atullchaurasia · x · 2026-11-29
A coding problem reportedly used in Anthropic's screening: given a text and a dictionary of <key>:<id> entries (unique keys, text length and dictionary size up to 10^9), split the text into tokens. At each position, pick the longest dictionary key that starts there, output its id, and skip past it; if no key matches, emit the single character as a literal and advance by one.
The suggested approach builds a trie over all keys, storing each key's id at its end node, then scans text left to right: at each position follow the trie as deep as the text matches, tracking the last node where a complete key ended. Output that key's id and jump past it, otherwise emit the current character. Time is O(N×L) where N is text length and L the longest key length; space is O(S+N) for the trie plus output. Follow-ups cover handling a dictionary too large for memory and compressing trie space when keys share prefixes.
More from coding & agent
- Claude leaks grader details into solver prompts during synthetic data generation — rickasaurus · 2026-11-29
- DeepMind's multi-agent recipe: verified shared notebook, 4-hour syncs, 11.8x compute savings — Dedap85 · 2026-11-29
- AI PM shares 7 lessons: state your goal, not the task; AI must close loops alone — Electrical_Pen_4989 · 2026-11-29
- Law firm builds case management app on Perplexity, every claim cites a medical record page — jeffgrimes9 · 2026-11-29
- DataTalksClub launches free course on production AI agents for generative media, recording now up — Al_Grigor · 2026-11-29
- Qwen-Image-Edit + Wan 2.2 Animate pipeline for AI character video replacement, face drift remains — MediumStop2150 · 2026-11-29