A Trie is a tree-like data structure used to store a dynamic set of strings where keys are usually strings. Each node represents a character, and paths from the root to leaves represent words.
Click to reveal answer
beginner
How does a Trie help in word search?
A Trie allows fast word search by following the path of characters from the root node. If the path for all characters of a word exists and ends at a node marked as a word end, the word is found.
Click to reveal answer
beginner
What does the 'isEndOfWord' flag represent in a Trie node?
The 'isEndOfWord' flag marks that the path from the root to this node forms a complete word stored in the Trie.
Click to reveal answer
intermediate
Explain the steps to search a word in a Trie.
Start at the root node. For each character in the word, check if a child node exists for that character. If not, the word is not in the Trie. If all characters are found, check if the last node's 'isEndOfWord' is true to confirm the word exists.
Click to reveal answer
intermediate
Why is Trie more efficient than a list for searching words?
Trie reduces search time by sharing common prefixes among words, allowing search in O(m) time where m is the word length, instead of O(n) where n is the number of words in a list.
Click to reveal answer
What does each node in a Trie represent?
AA whole word
BA sentence
CA number
DA character of a word
✗ Incorrect
Each node in a Trie represents a single character of a word.
What indicates that a node marks the end of a word in a Trie?
AA boolean flag like 'isEndOfWord'
BThe node has no children
CA special child node
DThe node stores the word
✗ Incorrect
The 'isEndOfWord' boolean flag marks the end of a word in a Trie node.
If a character path is missing in the Trie during search, what does it mean?
AThe word does not exist
BThe word exists
CThe Trie is empty
DThe word is a prefix
✗ Incorrect
Missing character path means the word is not stored in the Trie.
What is the time complexity to search a word of length m in a Trie?
AO(m * n)
BO(n)
CO(m)
DO(log n)
✗ Incorrect
Searching a word in a Trie takes O(m) time, where m is the length of the word.
Which of these is a benefit of using a Trie for word search?
AUses less memory than arrays
BShares common prefixes to speed up search
CStores words in sorted order automatically
DAllows searching numbers only
✗ Incorrect
Trie shares common prefixes among words, making search faster.
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 searching words compared to lists.
Consider how common parts of words are stored once.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using a Trie data structure in word search?
easy
A. To efficiently store and search words by their prefixes
B. To sort words alphabetically
C. To store words in a hash table
D. To perform arithmetic operations on words
Solution
Step 1: Understand Trie structure
A Trie stores characters of words in a tree where each node represents a character.
Step 2: Identify Trie usage
It allows quick prefix-based search by traversing nodes matching characters.
Final Answer:
To efficiently store and search words by their prefixes -> Option A
Quick Check:
Trie = prefix search [OK]
Hint: Trie is all about prefix-based fast word search [OK]
Common Mistakes:
Confusing Trie with hash tables
Thinking Trie sorts words
Assuming Trie does arithmetic
2. Which of the following is the correct way to check if a word exists in a Trie in JavaScript?
easy
A. Use a loop to traverse nodes matching each character and check end flag
B. Use Array.includes(word) on the Trie object
C. Use JSON.stringify on Trie and search substring
D. Use Math.max on character codes of the word
Solution
Step 1: Understand Trie search method
Search involves traversing nodes for each character and checking if the word ends at last node.
Step 2: Eliminate incorrect options
Array.includes and JSON.stringify are not suitable for Trie structure. Math.max is unrelated.
Final Answer:
Use a loop to traverse nodes matching each character and check end flag -> Option A
Quick Check:
Traverse nodes + check end = correct search [OK]
Hint: Traverse each character node and check end flag [OK]
Common Mistakes:
Using array methods on Trie
Trying to convert Trie to string
Applying math functions on words
3. What will be the output of the following code snippet?
class TrieNode {
constructor() {
this.children = {};
this.isEnd = false;
}
}
class Trie {
constructor() {
this.root = new TrieNode();
}
insert(word) {
let node = this.root;
for (const char of word) {
if (!node.children[char]) node.children[char] = new TrieNode();
node = node.children[char];
}
node.isEnd = true;
}
search(word) {
let node = this.root;
for (const char of word) {
if (!node.children[char]) return false;
node = node.children[char];
}
return node.isEnd;
}
}
const trie = new Trie();
trie.insert('cat');
trie.insert('car');
console.log(trie.search('cat'));
console.log(trie.search('cap'));
medium
A. false false
B. false true
C. true false
D. true true
Solution
Step 1: Insert words 'cat' and 'car'
Both words are added character by character, marking end nodes for 't' and 'r'.
Step 2: Search 'cat' and 'cap'
'cat' exists with isEnd true, 'cap' path breaks at 'p' missing node, returns false.
Final Answer:
true false -> Option C
Quick Check:
Search 'cat' = true, 'cap' = false [OK]
Hint: Check inserted words and search path carefully [OK]
Common Mistakes:
Assuming 'cap' exists after inserting 'car'
Confusing isEnd flag
Ignoring missing child nodes
4. Identify the bug in the following Trie search method and select the fix:
search(word) {
let node = this.root;
for (const char of word) {
if (!node.children[char]) return false;
node = node.children[char];
}
return true;
}
medium
A. Return false at the end instead of true
B. Add a check for empty word before loop
C. Change for loop to while loop
D. Return node.isEnd instead of true to confirm full word match
Solution
Step 1: Analyze current search return
Returns true if path exists but does not check if word ends here.
Step 2: Correct return value
Must return node.isEnd to confirm the word is complete, not just prefix.
Final Answer:
Return node.isEnd instead of true to confirm full word match -> Option D
Quick Check:
Check isEnd flag to confirm word end [OK]
Hint: Always check isEnd flag to confirm full word [OK]
Common Mistakes:
Returning true without checking isEnd
Changing loop type unnecessarily
Ignoring empty word cases
5. Given a list of words, how can you use a Trie to find all words that start with a given prefix efficiently? Choose the best approach.
hard
A. Sort words and use binary search on prefix
B. Insert all words into Trie, traverse prefix nodes, then collect all descendant words
C. Use a hash map with prefix keys for direct lookup
D. Check each word in list if it starts with prefix using string methods
Solution
Step 1: Insert all words into Trie
Build Trie so each word is represented by a path of nodes.
Step 2: Traverse prefix nodes and collect words
Find node matching prefix end, then recursively collect all words below it.
Step 3: Compare other options
Sorting and binary search is possible but less efficient for many queries. Hash maps need many keys. Checking each word is slow.
Final Answer:
Insert all words into Trie, traverse prefix nodes, then collect all descendant words -> Option B