A Trie is a tree-like data structure that stores a dynamic set of strings, where each node represents a character. It is used to efficiently search for words or prefixes.
Click to reveal answer
beginner
How does a Trie help in word search?
A Trie allows quick lookup of words by following the path of characters from the root to leaf nodes, making word search operations efficient especially for prefix matching.
Click to reveal answer
beginner
What does each node in a Trie typically contain?
Each node contains an array or map of child nodes for each possible character and a boolean flag indicating if the node marks the end of a valid word.
Click to reveal answer
intermediate
Explain the process of searching a word in a Trie.
Start from the root node, for each character in the word, move to the corresponding child node. If at any point the child node doesn't exist, the word is not present. If all characters are found and the last node marks end of word, the word exists.
Click to reveal answer
intermediate
Why is Trie preferred over HashMap for prefix-based word search?
Trie supports prefix search efficiently by traversing nodes character by character, while HashMap requires checking all keys or additional structures, making Trie faster for prefix queries.
Click to reveal answer
What does a boolean flag in a Trie node represent?
AIf the node is the root
BIf the node is a leaf node
CIf the node has children
DIf the node marks the end of a valid word
✗ Incorrect
The boolean flag indicates whether the node marks the end of a valid word stored in the Trie.
What happens if a character in the search word is not found in the Trie?
ARestart search from root
BContinue searching next character
CReturn false, word not found
DMark node as end of word
✗ Incorrect
If a character is missing in the Trie path, the word does not exist in the Trie.
Which operation is fastest with a Trie compared to a HashMap?
APrefix search
BExact word search
CInsertion of words
DDeletion of words
✗ Incorrect
Trie is optimized for prefix search by traversing nodes character by character.
What is the time complexity of searching a word in a Trie of length n?
AO(n)
BO(n^2)
CO(log n)
DO(1)
✗ Incorrect
Searching a word takes time proportional to the length of the word, O(n).
In a Trie, what does each edge represent?
AA word
BA character
CA node
DA prefix
✗ Incorrect
Each edge in a Trie represents a character connecting nodes.
Describe how to search for a word in a Trie step-by-step.
Think about following the path of characters from root to leaf.
You got /4 concepts.
Explain why Tries are efficient for prefix searches compared to other data structures.
Consider how prefixes share common paths in the Trie.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a Trie data structure in word search problems?
easy
A. To perform arithmetic operations on words
B. To sort words alphabetically
C. To store words in a linked list
D. To efficiently store and search words by their prefixes
Solution
Step 1: Understand Trie structure
A Trie stores characters of words in a tree-like structure where each node represents a character.
Step 2: Identify Trie usage in word search
It allows quick prefix-based search, making it efficient to find words starting with given letters.
Final Answer:
To efficiently store and search words by their prefixes -> Option D
Quick Check:
Trie = prefix search [OK]
Hint: Trie is all about prefix-based fast word search [OK]
Common Mistakes:
Confusing Trie with sorting data structures
Thinking Trie stores words as linked lists
Assuming Trie performs arithmetic on words
2. Which of the following is the correct way to declare a Trie node class in Java?
easy
A. class TrieNode { char val; TrieNode[] children = new TrieNode[26]; boolean isWord; }
B. class TrieNode { int val; TrieNode children; boolean isWord; }
C. class TrieNode { String val; TrieNode[] children = new TrieNode[26]; }
D. class TrieNode { char val; List<TrieNode> children; boolean isWord; }
Solution
Step 1: Check data types for Trie node
Each node stores a character, an array of 26 children for letters a-z, and a boolean to mark word end.
Step 2: Validate options
class TrieNode { char val; TrieNode[] children = new TrieNode[26]; boolean isWord; } matches this structure exactly with char val, TrieNode[] children of size 26, and boolean isWord.
Final Answer:
class TrieNode { char val; TrieNode[] children = new TrieNode[26]; boolean isWord; } -> Option A
Quick Check:
Correct TrieNode declaration = class TrieNode { char val; TrieNode[] children = new TrieNode[26]; boolean isWord; } [OK]
Hint: Trie nodes use char + array of 26 children + boolean flag [OK]
Common Mistakes:
Using int or String instead of char for node value
Using single child instead of array for children
Using List instead of fixed-size array for children
3. What will be the output of the following Java code snippet that inserts words into a Trie and searches for "cat"?
class TrieNode {
TrieNode[] children = new TrieNode[26];
boolean isWord = false;
}
class Trie {
TrieNode root = new TrieNode();
void insert(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
int idx = c - 'a';
if (node.children[idx] == null) node.children[idx] = new TrieNode();
node = node.children[idx];
}
node.isWord = true;
}
boolean search(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
int idx = c - 'a';
if (node.children[idx] == null) return false;
node = node.children[idx];
}
return node.isWord;
}
}
public class Main {
public static void main(String[] args) {
Trie trie = new Trie();
trie.insert("cat");
trie.insert("car");
System.out.println(trie.search("cat"));
System.out.println(trie.search("cap"));
}
}
medium
A. false
false
B. true
false
C. true
true
D. false
true
Solution
Step 1: Insert words "cat" and "car" into Trie
Both words are inserted character by character, marking the last node's isWord as true.
Step 2: Search for "cat" and "cap"
"cat" exists with isWord true, so search returns true. "cap" path is missing 'p' node, so returns false.
Final Answer:
true
false -> Option B
Quick Check:
Inserted words found = true, missing word = false [OK]
Hint: Search returns true only if full word path and isWord true [OK]
Common Mistakes:
Assuming partial prefix returns true
Not marking isWord correctly on insert
Confusing character indexing in children array
4. Identify the bug in the following Trie search method and choose the correct fix:
boolean search(String word) {
TrieNode node = root;
for (int i = 0; i <= word.length(); i++) {
int idx = word.charAt(i) - 'a';
if (node.children[idx] == null) return false;
node = node.children[idx];
}
return node.isWord;
}
medium
A. Initialize idx before loop
B. Return true inside the loop
C. Change loop condition to i < word.length()
D. Remove return statement inside loop
Solution
Step 1: Analyze loop boundary
The loop runs from i = 0 to i <= word.length(), which causes index out of bounds at last iteration.
Step 2: Fix loop condition
Changing condition to i < word.length() prevents accessing invalid index and correctly iterates over characters.
Final Answer:
Change loop condition to i < word.length() -> Option C
Quick Check:
Loop boundary fix prevents index error [OK]
Hint: Loop over string chars with i < length, not <= length [OK]
Common Mistakes:
Using <= instead of < in loop condition
Trying to fix by moving idx initialization
Removing return inside loop causing wrong logic
5. Given a 2D board of characters and a list of words, which approach best uses a Trie to efficiently find all words present on the board?
A sample board: