Trie (Prefix Tree)

Data Structures

Efficient prefix matching, autocomplete, and dictionary lookups

53 problems·4 Easy·27 Medium·22 Hard
Pattern Study Guide & Cheat Sheet

Tree structure where paths from root represent string prefixes, allowing prefix searches, autocomplete, and dictionary lookups in O(L) time proportional to word length.

Core Invariant: Each node represents a character transition. A node contains children `char -> TrieNode` and a boolean flag `is_end = True` indicating if a complete word terminates at that node.

Recognize it (Keywords & Signals)
  • Prefix search / implement startsWith(prefix)
  • Word search board with dictionary / Boggle game
  • Autocomplete / search suggestions / spell checker
  • Replace words with shortest root
  • Maximum XOR pair in an array (Binary Bitwise Trie)
When NOT to use

Exact lookups only with no prefix queries (standard Hash Set has less memory overhead and O(1) expected time).

How to solve (Step-by-step)
  1. 1.Define `TrieNode` with `children = {}` and `is_end = False`.
  2. 2.Insert: walk character by character. If character not in `curr.children`, create new node. Mark last node `is_end = True`.
  3. 3.Search: walk characters. If any character missing, return False. If all found, return `curr.is_end`.
  4. 4.StartsWith: walk characters. If any missing, return False. If found, return True regardless of `is_end`.
Watch for (Interview Traps)
  • Confusing `search` (requires `node.is_end == True`) with `starts_with` (only requires node exists)
  • Memory explosion: in Python, using `dict` children is more memory-efficient than `[None] * 26`
  • Word Search II TLE: forget to remove found words from Trie or mark already visited grid cells
Prefix Tree (Trie) Implementation
class TrieNode:
    def __init__(self):
        self.children: dict[str, TrieNode] = {}
        self.is_end: bool = False

class Trie:
    def __init__(self):
        self.root = TrieNode()

    def insert(self, word: str) -> None:
        curr = self.root
        for char in word:
            if char not in curr.children:
                curr.children[char] = TrieNode()
            curr = curr.children[char]
        curr.is_end = True

    def search(self, word: str) -> bool:
        node = self._traverse(word)
        return node is not None and node.is_end

    def starts_with(self, prefix: str) -> bool:
        return self._traverse(prefix) is not None

    def _traverse(self, prefix: str) -> TrieNode | None:
        curr = self.root
        for char in prefix:
            if char not in curr.children:
                return None
            curr = curr.children[char]
        return curr
Cost
O(L) per insert/search where L is word length · O(total characters * alphabet size) (Lookup speed is completely independent of the number of words stored in the Trie.)
Canonical problems
#208 Implement Trie (Prefix Tree): Foundational insert, search, and startsWith operations
#211 Design Add and Search Words Data Structure: DFS on Trie branches to handle "." wildcard queries
#212 Word Search II: Traverse 2D matrix with Trie to prune invalid prefixes immediately
#421 Maximum XOR of Two Numbers in an Array: Bitwise 0/1 Trie picks opposite bits greedily
Easy·62 companies·Max freq 100%·Acc 47.9%
CME GroupUSAAAlten+59
Medium·36 companies·Max freq 100%·Acc 49.6%
CoupangSquareBuyHatke+33
Medium·25 companies·Max freq 100%·Acc 0.7%
XMongoDBinstabase+22
Medium·24 companies·Max freq 100%·Acc 60.4%
RobinhoodBoxZynga+21
Hard·24 companies·Max freq 89%·Acc 38.6%
SnowflakeCiscoTwo Sigma+21
Medium·19 companies·Max freq 100%·Acc 65.2%
Squarepoint CapitalUBSDoorDash+16
Hard·19 companies·Max freq 100%·Acc 55.8%
XDropboxX+16
Medium·17 companies·Max freq 100%·Acc 62.3%
The Trade DeskZipRecruiterVisa+14
Hard·16 companies·Max freq 100%·Acc 48.3%
NetskopeBaiduSnowflake+13
Medium·16 companies·Max freq 88%·Acc 48.7%
DatadogNeetCode 150NeetCode 150+13
Hard·13 companies·Max freq 75%·Acc 50.0%
RobloxPinterestMongoDB+10
Medium·12 companies·Max freq 75%·Acc 65.2%
AirbnbDoorDashCapital One+9
Medium·9 companies·Max freq 100%·Acc 50.6%
BlendspinnyVisa+6
Medium·8 companies·Max freq 88%·Acc 78.6%
VerkadaNuroSnowflake+5
Hard·8 companies·Max freq 88%·Acc 37.4%
AirbnbWixExpedia+5
Easy·7 companies·Max freq 100%·Acc 77.8%
AutodeskFICOCapital One+4
Hard·7 companies·Max freq 100%·Acc 46.4%
HuluDE ShawGoogle+4
Hard·7 companies·Max freq 88%·Acc 77.4%
Booking.comBooking.comNvidia+4
Medium·6 companies·Max freq 53%·Acc 76.3%
BarclaysBloombergGoogle+3
Hard·5 companies·Max freq 88%·Acc 0.6%
SnapSnapApplied Intuition+2
Medium·5 companies·Max freq 38%·Acc 53.6%
GoogleBloombergAmazon+2
Hard·4 companies·Max freq 100%·Acc 28.7%
AutodeskSamsungCapital One+1
Medium·4 companies·Max freq 100%·Acc 64.7%
DunzoIntuitUber+1
Medium·4 companies·Max freq 78%·Acc 51.4%
GustoTikTokMeta+1
Medium·4 companies·Max freq 75%·Acc 55.1%
PinterestAmazonGoogle+1
Hard·4 companies·Max freq 63%·Acc 52.6%
Jane StreetGoogleMeta+1
Hard·4 companies·Max freq 63%·Acc 59.6%
AtlassianAmazonMicrosoft+1
Medium·4 companies·Max freq 52%·Acc 68.9%
UberTikTokMicrosoft+1
Medium·4 companies·Max freq 25%·Acc 73.4%
MetaGoogleBloomberg+1
Medium·3 companies·Max freq 100%·Acc 51.1%
MoveworksAffirmAirbnb
Hard·3 companies·Max freq 100%·Acc 60.8%
BoeingGoogleAmazon
Hard·3 companies·Max freq 100%·Acc 38.7%
DuolingoAmazonGoogle
Medium·3 companies·Max freq 100%·Acc 57.3%
Akuna CapitalFlipkartGoogle
Hard·3 companies·Max freq 53%·Acc 49.9%
AmazoneBayTikTok
Medium·3 companies·Max freq 52%·Acc 55.1%
TuringUberAmazon
Hard·3 companies·Max freq 25%·Acc 41.0%
MetaMicrosoftGoogle
Medium·3 companies·Max freq 15%·Acc 57.4%
GoogleMetaAmazon
Medium·2 companies·Max freq 75%·Acc 65.6%
CompassGoogle
Medium·2 companies·Max freq 45%·Acc 58.0%
BloombergGoogle
Hard·2 companies·Max freq 25%·Acc 52.6%
GoogleAmazon
Medium·2 companies·Max freq 13%·Acc 63.5%
AmazonMicrosoft
Easy·1 companies·Max freq 100%·Acc 76.3%
ZScaler
Hard·1 companies·Max freq 100%·Acc 33.2%
ZScaler
Hard·1 companies·Max freq 100%·Acc 46.5%
Vimeo
Hard·1 companies·Max freq 100%·Acc 47.9%
Dropbox
Medium·1 companies·Max freq 30%·Acc 60.9%
Apple
Easy·1 companies·Max freq 25%·Acc 68.7%
Amazon
Hard·1 companies·Max freq 25%·Acc 58.9%
Google
Medium·1 companies·Max freq 25%·Acc 59.4%
Google
Hard·1 companies·Max freq 25%·Acc 53.3%
Google
Hard·1 companies·Max freq 25%·Acc 54.7%
Google
Medium·1 companies·Max freq 25%·Acc 52.5%
Google
Medium·1 companies·Max freq 25%·Acc 72.1%
Google
Showing 53 of 53 problems in Trie (Prefix Tree)Filtered: All Companies