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

Lesson 13.2 of 6 in this module Unrolled Pro

Implement Trie

Build a prefix tree with insert, search (exact word) and startsWith (any word with this prefix). From a list scanned on every call (O(N · L)), to a hash set that makes search O(L) but leaves startsWith as a scan, to a trie where all three operations are O(L) with an end-of-word flag separating words from prefixes. Then delete with pruning, counts for duplicates, array children, and constrained decoding: using the trie's children as the only allowed next tokens so a model can only output valid labels.

about 10 min 3 Python examples 2 quick checks

Problem. Implement a class with three operations:

  • insert(word): add the word;
  • search(word): return True if the exact word was inserted;
  • startsWith(prefix): return True if any inserted word starts with the prefix.
text
insert("apple")
search("apple")     →  True
search("app")       →  False     ("app" is only a prefix so far)
startsWith("app")   →  True
insert("app")
search("app")       →  True

The difference between search("app") and startsWith("app") is the whole design: both walk the same path, but only search asks whether a word ends at the last node. That one flag per node is what separates "a word" from "the beginning of a word".

ML context. A trie over a label set or vocabulary is how systems restrict outputs to valid strings: constrained decoding lets a language model generate only class names, product IDs or entity names that exist, by allowing at each step only the characters (or tokens) that are children of the current trie node (Change D). The same structure maps strings to token IDs in tokenizers.


1. Step 1: Clarify

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

Unlock "Implement Trie"

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