208. Implement Trie (Prefix Tree) - Detailed Explanation

Problem statement

Implement the Trie class:

  • Trie() Initializes the trie object.
  • void insert(String word) Inserts the string word into the trie.
  • boolean search(String word) Returns true if the exact string word is in the trie (i.e., was previously inserted), or false otherwise.
  • boolean startsWith(String prefix) Returns true if there is any previously inserted string that starts with the given prefix, or false otherwise.

Examples

Input ["Trie","insert","search","search","startsWith","insert","search"] [[], ["apple"],["apple"],["app"], ["app"], ["app"],["app"]] Output [null, null, true, false, true, null, true] Explanation Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // returns true trie.search("app"); // returns false (only prefix "app" exists) trie.startsWith("app"); // returns true trie.insert("app"); trie.search("app"); // returns true

Constraints

  • 1 ≤ word.length, prefix.length ≤ 2000
  • word and prefix consist only of lowercase English letters.
  • At most 3·10⁴ calls in total will be made to insert, search, and startsWith.

Approach

This question names its own pattern: the Trie, also called a prefix tree. A trie is a tree in which the path from the root to a node spells a string. Every stored word is therefore a route through the tree rather than a value sitting in a slot, and words that share a prefix share that part of the route. The structure is one of the standard data structures behind the recurring coding interview patterns, and it is the answer whenever an interviewer asks about prefixes rather than whole keys.

A hash set looks tempting here, and it does answer search in O(1) on average. It cannot answer startsWith without inspecting every key it holds. The trie turns that query into a walk of at most m steps, where m is the length of the prefix, which is the whole reason to build one.

Trie node structure

Each node holds:

  • An array (or map) of up to 26 child pointers, one for each lowercase letter.
  • A boolean flag isEnd indicating whether a word ends at this node.

The isEnd flag is what separates a stored word from a passing prefix. Inserting "apple" creates five nodes and sets isEnd only on the last one. So search("app") reaches a real node and still returns false, because that node is only a waypoint, while startsWith("app") returns true from the same node. Insert "app" later and the flag on that existing node flips to true, with no new nodes allocated.

Operations

insert(word)

  1. Start at the root node.
  2. For each character c in word:
    • Compute its index i = c - 'a'.
    • If node.children[i] is null, create a new TrieNode.
    • Move node = node.children[i].
  3. After the last character, mark node.isEnd = true.

search(word)

  1. Traverse exactly as in insert, but if at any step node.children[i] is null, return false.
  2. After processing all chars, return node.isEnd.

startsWith(prefix)

  1. Traverse as in search, but do not check isEnd.
  2. If you complete the traversal without hitting null, return true.

All three operations are the same walk over one string, differing only in what they do when a child link is missing and in what they report at the end. Write the walk once as a private helper that returns the node reached or null, then express search and startsWith on top of it. Grokking the Coding Interview builds the class in that order in its Implement Trie lesson, which is where the problem and the rest of the trie pattern are taught.

Complexity analysis

  • Time: O(m) per operation (m = length of word or prefix).
  • Space: In the worst case, O(N·L) for storing all inserted words (N words of average length L), since each character may create a new node.

Python code

Python3
Python3

. . . .

Java code

Java
Java

. . . .

Common mistakes

  • Forgetting to mark isEnd on insert, causing search to return false for inserted words.
  • Using a map per node instead of a fixed-size array, which increases constant factors.
  • In startsWith, accidentally requiring isEnd to be true.
  • Allocating the 26-slot child array in every node before you need it, which wastes memory on sparse branches. Allocate a child only when a character first needs it.

Edge cases

  • Inserting or searching the same word multiple times.
  • Searching for a prefix that is exactly an inserted word.
  • Very long words near the maximum length.
  • A search or startsWith call on an empty trie, which must return false on the first missing child link rather than fail.

Prefix queries scale up quickly once the class exists. In Counting Words With a Given Prefix you report the number of matches instead of a yes or no, which a counter stored per node answers in the same walk. Then Word Search II pairs the trie with depth-first search and uses it to prune dead branches of the grid. For a question built directly on this class, try Search Suggestions System, which returns the first three matching products for every prefix of a search term. All three reward the same groundwork, and Grokking the Coding Interview collects them into one chapter.

TAGS
Coding Interview Questions
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What does a full stack developer portfolio look like?
What Is Stripe's Tech Stack? Languages, Databases, and Cloud
Stripe runs a mostly Ruby backend on Amazon Web Services, with Scala, Go and Java in supporting systems and a document database built on MongoDB.
What is the main job of backend developer?
What is an operating system interview question?
What Is the Synthesia Interview Process Like? (Round by Round)
Synthesia's job postings list five stages: a recruiter call, a past projects talk, a take-home or live coding, a technical discussion, and a leadership call.
How to immigrate to the UK as a software engineer?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$99

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.