Trie & Inverted Index
The Search Bar That Stalled on Every Keystroke
Test your architecture intuition: Pitch a 7-axis solution, survive two aggressive reviewer objections, and inspect the staff-level Teacher Gold Answer.
1. What It Is & Why It Exists
The Core Problem: Prefix Matching & Arbitrary Full-Text Search
Relational databases index scalar values with B-Trees, which support exact match and prefix queries (WHERE name LIKE 'app%') in time. However, modern search architectures require:
- Ultra-Low Latency Autocomplete ( at ): Finding top-K suggestions matching a typed prefix regardless of total dictionary size.
- Arbitrary Substring & Multi-Term Search (
LIKE '%system%design%'): Querying documents containing multiple keywords in any order across billions of web pages. Standard B-Trees cannot perform substring searches without full table scans.
The First-Principles Solution: Specialized Information Retrieval Structures
- Trie (Prefix Tree): A tree data structure where keys are decomposed along characters. Search time is strictly , where is the length of the query prefix, completely decoupled from total dictionary size .
- Inverted Index: An associative data structure that maps every unique dictionary term (word) to a sorted list of integer document identifiers (Posting List), enabling fast set intersections and full-text relevance ranking.
Synthesizing vector architecture diagram...
The two panels answer two different search questions. In the "Autocomplete Prefix Trie" panel, a user types s-y-s and the lookup walks one node per letter from the root; the node for 'sys' already stores its top 5 completions, so the suggestions come back after 3 steps no matter how many phrases exist. Typing on to 'system' just continues down the same path to a node with its own top 5. In the "Inverted Index with BM25 Scoring" panel, each word maps to a sorted list of document IDs; a query for "distributed consensus" walks the two lists in parallel with two pointers, keeps the IDs in both (42 and 105), and then ranks them with BM25. Tries answer "what starts with this?" and inverted indexes answer "which documents contain these words?"
2. Mathematical Foundations & Algorithmic Mechanics
1. The Okapi BM25 Ranking Formula
Modern search engines (Elasticsearch, Amazon OpenSearch, Lucene) rank matched documents using the Okapi BM25 probabilistic relevance score:
Where:
- : Inverse Document Frequency penalizing ubiquitous terms (e.g. "the", "is").
- : Term frequency of keyword in document .
- : Document length normalized against average corpus length.
- : Standard saturation tuning parameters.
2. Posting List Intersection Time Complexity
Given two sorted posting lists of lengths and ():
- Linear Two-Pointer Intersection: comparisons.
- Galloping (Exponential) Search: comparisons: for each entry of the short list, jump ahead in the long list by doubling steps, then binary search. Skip pointers (a textbook heuristic places one every entries) let the long list jump ahead in a similar way.
- Roaring Bitmaps: Dense chunks are stored as bitmaps and intersected with word-wide (and SIMD) bitwise
AND, many document IDs per instruction.
Unlock Complete Architecture & Production Runbooks
You have explored the free architectural preview (~44%). Spend 1 Coin to unlock the remaining 5 production deep-dive sections for a full 24 hours.