Trie (Prefix Tree)

Data Structures

Efficient prefix matching, autocomplete, and dictionary lookups

51 problems·4 Easy·26 Medium·21 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%
Publicis SapientUSAAEricsson+59
Medium·26 companies·Max freq 100%·Acc 0.7%
instabaseXMongoDB+23
Hard·25 companies·Max freq 90%·Acc 38.6%
SnowflakeCiscoTwo Sigma+22
Medium·24 companies·Max freq 100%·Acc 60.4%
RobinhoodBoxZynga+21
Medium·19 companies·Max freq 100%·Acc 65.2%
Squarepoint CapitalUBSWix+16
Hard·19 companies·Max freq 100%·Acc 0.6%
XDropboxX+16
Medium·17 companies·Max freq 100%·Acc 62.3%
ZipRecruiterThe Trade DeskSIG+14
Medium·17 companies·Max freq 88%·Acc 0.5%
DatadogNeetCode 150NeetCode 150+14
Hard·13 companies·Max freq 75%·Acc 50.0%
RobloxMongoDBPinterest+10
Medium·12 companies·Max freq 75%·Acc 65.2%
AirbnbDoorDashCoinbase+9
Hard·8 companies·Max freq 88%·Acc 37.4%
AirbnbWixExpedia+5
Medium·8 companies·Max freq 88%·Acc 78.6%
VerkadaNuroSnowflake+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
Hard·6 companies·Max freq 88%·Acc 0.6%
SnapSnapThe Trade Desk+3
Medium·6 companies·Max freq 53%·Acc 76.3%
BarclaysBloombergGoogle+3
Medium·5 companies·Max freq 55%·Acc 68.9%
UberNvidiaTikTok+2
Medium·5 companies·Max freq 38%·Acc 53.6%
GoogleBloombergMicrosoft+2
Medium·4 companies·Max freq 100%·Acc 64.7%
DunzoIntuitUber+1
Hard·4 companies·Max freq 100%·Acc 28.7%
AutodeskSamsungCapital One+1
Medium·4 companies·Max freq 78%·Acc 51.4%
GustoTikTokMeta+1
Medium·4 companies·Max freq 75%·Acc 55.1%
PinterestGoogleAmazon+1
Hard·4 companies·Max freq 63%·Acc 52.6%
Jane StreetGoogleMeta+1
Hard·4 companies·Max freq 63%·Acc 59.6%
AtlassianMicrosoftGoogle+1
Medium·4 companies·Max freq 26%·Acc 73.4%
GoogleMetaBloomberg+1
Medium·3 companies·Max freq 100%·Acc 51.1%
MoveworksAffirmAirbnb
Hard·3 companies·Max freq 100%·Acc 38.7%
DuolingoGoogleAmazon
Hard·3 companies·Max freq 100%·Acc 52.2%
DirectiMedia.netMedia.net
Medium·3 companies·Max freq 100%·Acc 57.3%
Akuna CapitalFlipkartGoogle
Medium·3 companies·Max freq 55%·Acc 55.1%
TuringUberAmazon
Hard·3 companies·Max freq 53%·Acc 49.9%
AmazoneBayTikTok
Medium·3 companies·Max freq 15%·Acc 57.4%
GoogleMetaAmazon
Hard·2 companies·Max freq 88%·Acc 0.5%
Media.netMedia.net
Medium·2 companies·Max freq 75%·Acc 65.6%
CompassGoogle
Medium·2 companies·Max freq 63%·Acc 0.2%
Media.netMedia.net
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%
MicrosoftAmazon
Hard·1 companies·Max freq 100%·Acc 46.5%
Vimeo
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 47.9%
Dropbox
Medium·1 companies·Max freq 30%·Acc 60.9%
Apple
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
50 displayed (of 50 matching)Filtered: All Companies

Top Companies Testing Trie (Prefix Tree)

Browse company-specific interview problem lists and recency breakdowns:

Explore All 22 DSA Coding Patterns

Master core algorithmic patterns tested in FAANG & Big Tech coding interviews.

Patterns Hub

Trie (Prefix Tree) frequently asked questions

What is the Trie (Prefix Tree) pattern in coding interviews?

Efficient prefix matching, autocomplete, and dictionary lookups It is categorized under Data Structures and features 51 curated interview problems across Easy (4), Medium (26), and Hard (21) difficulties.

Which tech companies frequently test Trie (Prefix Tree)?

Companies frequently asking Trie (Prefix Tree) questions include Google, Amazon, Microsoft, Meta, Bloomberg, Uber.

How do I master the Trie (Prefix Tree) pattern?

Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.