VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 13: Tries and strings

Lesson 13.1 of 6 in this module Unrolled Pro

Tries: share the beginning, save the work

A trie stores strings as paths from a root, one character per edge, so words with a common beginning share the same nodes. On a running example, counting how many words start with a prefix: from scanning every word (O(N · L) per query), to binary search on a sorted list (O(L log N)), to hashing every prefix (fast but O(Σ L²) memory), to a trie that answers in O(L) with one node per distinct prefix. Then children as dicts or arrays, compressed tries, and longest-prefix matching, the move behind greedy subword tokenizers.

about 10 min 4 Python examples 2 quick checks

The question this module answers: what if you have many strings and keep asking questions about their beginnings? A hash set answers "is this exact word present?" in O(L), but it can't answer "which words start with inf?" without looking at every word. A trie (from retrieval, also called a prefix tree) stores strings so that every prefix is a node: the question "what starts with inf?" becomes "walk three edges and look below".

ML context.

  • Tokenizers: greedy longest-match subword tokenizers walk a trie of the vocabulary from each position (Section 4), and byte-pair encoding builds its vocabulary by merging frequent pairs (lessons 13.6 and 15.12).
  • Autocomplete and query suggestion: the top completions for a typed prefix (lesson 13.5).
  • Constrained decoding: forcing a language model to produce only valid labels or entity names by masking every token that doesn't continue a path in a trie of the allowed outputs.
  • Lookups by prefix: feature names, file paths, URL routing, IP prefix tables.

1. The running problem: count words with a prefix

Task: given N words, answer Q queries of the form "how many words start with prefix p?"

With N = 10⁶ words and Q = 10⁵ queries (an autocomplete box as people type), that's 10¹¹ character comparisons. Every query re-reads words that can't possibly match.

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Tries: share the beginning, save the work"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee