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

Lesson 13.5 of 6 in this module Unrolled Pro

Autocomplete from a prefix

Return the k most popular past queries that start with what the user has typed so far. From scanning and sorting every query on each keystroke (O(N · L + M log M)), to a trie that walks the prefix and searches only its subtree with a heap (O(|p| + subtree)), to storing each node's current top k so a keystroke costs O(|p| + k). Then counts that keep growing, recency decay, typos, personalisation, and re-ranking a short candidate list with a model.

about 10 min 3 Python examples 2 quick checks

Problem. You have a log of past queries with their counts. As a user types, return the k most frequent past queries that start with the typed prefix (ties broken alphabetically). New searches keep arriving, so counts grow over time.

text
counts = {"transformer": 50, "transformers tutorial": 30, "train test split": 45,
          "tranformer": 3, "tree": 20, "trie": 8}
top_k("tra", k=3)    →  ["transformer", "train test split", "transformers tutorial"]
top_k("tri", k=3)    →  ["trie"]

Every keystroke is a new query, and suggestions must come back in a few milliseconds. The shape of the solution: walk to the prefix node in O(|p|), then answer from that node only, ideally from an answer already stored there.

ML context. This is the candidate-generation step of query suggestion in search engines, code completion and chat input boxes. Production systems generate a short list of prefix matches from a trie like this one, then re-rank it with a model that knows the user, the context and recency (Change D). The trie keeps the expensive model away from millions of candidates.


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 "Autocomplete from a prefix"

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