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 root to nodes represent prefixes of words.
Click to reveal answer
beginner
How does a Trie help in word search?
A Trie allows fast prefix-based search by traversing nodes corresponding to each character of the word. If traversal reaches the end of the word and the node marks the end of a word, the word exists in the Trie.
Click to reveal answer
beginner
What does the 'end of word' marker in a Trie node signify?
It indicates that the path from the root to this node forms a complete valid word stored in the Trie.
Click to reveal answer
intermediate
In TypeScript, how do you represent children nodes in a Trie node?
Children nodes are often represented as a Map or an object with keys as characters and values as TrieNode references.
Click to reveal answer
intermediate
What is the time complexity of searching a word in a Trie?
The time complexity is O(m), where m is the length of the word being searched, because you traverse one node per character.
Click to reveal answer
What does each node in a Trie represent?
AA character of a word
BA whole word
CA number
DA sentence
✗ Incorrect
Each node in a Trie represents a single character of a word.
How do you know if a word exists in a Trie after traversal?
AIf the first node is not null
BIf the last node visited marks the end of a word
CIf the Trie has more than one node
DIf the word length is greater than zero
✗ Incorrect
A word exists if the last node in traversal has an end-of-word marker.
What data structure is commonly used to store children in a Trie node in TypeScript?
AMap<string, TrieNode>
BArray<number>
CSet<string>
DBoolean
✗ Incorrect
A Map is used to map characters to child nodes.
What is the main advantage of using a Trie for word search?
ASorts words automatically
BUses less memory than arrays
CStores numbers efficiently
DFast prefix-based search
✗ Incorrect
Trie allows fast search by prefixes, making word search efficient.
What is the worst-case time complexity to search a word of length m in a Trie?
AO(m^2)
BO(log m)
CO(m)
DO(1)
✗ Incorrect
Searching requires checking each character once, so O(m).
Explain how to search for a word in a Trie step-by-step.
Think about walking down the tree one character at a time.
You got /5 concepts.
Describe the structure of a Trie node used for word search.
Focus on what each node holds to help find words.
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 efficiently store and search words by their prefixes
B. To sort words alphabetically
C. To store words in a linked list format
D. To perform arithmetic operations on words
Solution
Step 1: Understand the Trie structure
A Trie stores characters in a tree where each path represents a prefix of words.
Step 2: Identify the use in word search
This structure allows quick prefix-based search, making it efficient for word search problems.
Final Answer:
To efficiently store and search words by their prefixes -> Option A
Quick Check:
Trie = prefix search [OK]
Hint: Trie is all about prefixes and fast word lookup [OK]
Common Mistakes:
Confusing Trie with sorting structures
Thinking Trie stores words linearly
Assuming Trie performs arithmetic
2. Which of the following is the correct TypeScript syntax to declare a Trie node class with a children map and an end-of-word boolean?
easy
A. class TrieNode { children: Map; isEndOfWord: boolean; constructor() { this.children = new Map(); this.isEndOfWord = false; } }
B. class TrieNode { children: string[]; isEndOfWord: number; constructor() { this.children = []; this.isEndOfWord = 0; } }
C. class TrieNode { children: Map; isEndOfWord: boolean; constructor() { this.children = new Map(); this.isEndOfWord = true; } }
D. class TrieNode { children: object; isEndOfWord: string; constructor() { this.children = {}; this.isEndOfWord = 'false'; } }
Solution
Step 1: Check children type
Children should be a Map from string to TrieNode for efficient character mapping.
Step 2: Check isEndOfWord type and initialization
It should be a boolean initialized to false to mark word endings correctly.
Final Answer:
class TrieNode { children: Map; isEndOfWord: boolean; constructor() { this.children = new Map(); this.isEndOfWord = false; } } -> Option A
Quick Check:
Correct types and initialization [OK]
Hint: Use Map and boolean for end flag [OK]
Common Mistakes:
Using array instead of Map for children
Using wrong types for isEndOfWord
Initializing isEndOfWord incorrectly
3. Given the following TypeScript code snippet for searching a word in a Trie, what will be the output when searching for the word "cat" if only "car" and "cap" are inserted?
class TrieNode {
children = new Map();
isEndOfWord = false;
}
class Trie {
root = new TrieNode();
insert(word: string) {
let node = this.root;
for (const ch of word) {
if (!node.children.has(ch)) {
node.children.set(ch, new TrieNode());
}
node = node.children.get(ch)!;
}
node.isEndOfWord = true;
}
search(word: string): boolean {
let node = this.root;
for (const ch of word) {
if (!node.children.has(ch)) return false;
node = node.children.get(ch)!;
}
return node.isEndOfWord;
}
}
const trie = new Trie();
trie.insert("car");
trie.insert("cap");
console.log(trie.search("cat"));
medium
A. true
B. false
C. undefined
D. Runtime error
Solution
Step 1: Trace insertion of "car" and "cap"
Both words create nodes for 'c', 'a', then diverge at 'r' and 'p' respectively, marking those ends as true.
Step 2: Trace search for "cat"
Search finds 'c' and 'a', but 't' is not a child of 'a', so search returns false.
Final Answer:
false -> Option B
Quick Check:
Word not inserted = false [OK]
Hint: If last char missing, search returns false [OK]
Common Mistakes:
Assuming partial prefix means true
Confusing isEndOfWord with node existence
Expecting undefined instead of false
4. Identify the bug in the following TypeScript Trie search method that causes it to always return false:
search(word: string): boolean {
let node = this.root;
for (const ch of word) {
if (!node.children.has(ch)) return false;
node = node.children.get(ch)!;
}
return node.isEndOfWord === false;
}
medium
A. The method should return true before the loop ends
B. The loop should iterate backwards over the word
C. The children map should be an array instead
D. The return condition should be node.isEndOfWord, not its negation
Solution
Step 1: Analyze the return statement
The method returns node.isEndOfWord === false, which means it returns true if the word is NOT ending here.
Step 2: Correct the return logic
It should return node.isEndOfWord to confirm the word ends at this node.
Final Answer:
The return condition should be node.isEndOfWord, not its negation -> Option D
Quick Check:
Return true if word ends here [OK]
Hint: Return node.isEndOfWord directly to confirm word end [OK]
Common Mistakes:
Returning negation of isEndOfWord
Changing loop direction unnecessarily
Using wrong data structure for children
5. You want to implement a function that finds all words in a Trie starting with a given prefix. Which approach correctly combines Trie traversal and backtracking to collect all matching words?
hard
A. Search each word in the Trie individually to check prefix match
B. Store all words in an array and filter by prefix each time
C. Traverse Trie nodes for prefix, then recursively collect words from that node using backtracking
D. Use a hash map to map prefixes to words without Trie traversal
Solution
Step 1: Traverse Trie to prefix node
Follow characters of prefix to reach the node representing last prefix character.
Step 2: Use backtracking from prefix node
Recursively explore all children nodes to collect complete words starting with prefix.
Final Answer:
Traverse Trie nodes for prefix, then recursively collect words from that node using backtracking -> Option C
Quick Check:
Prefix traversal + backtracking = Traverse Trie nodes for prefix, then recursively collect words from that node using backtracking [OK]
Hint: Find prefix node, then backtrack to collect words [OK]
Common Mistakes:
Checking all words instead of prefix traversal
Filtering array instead of Trie traversal
Using hash map without Trie structure
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.