A Trie is a tree-like data structure that stores a dynamic set of strings, where each node represents a character. It is used for efficient retrieval of words, especially for prefix-based searches.
Click to reveal answer
intermediate
How does a Trie help in finding the longest word in a dictionary?
By inserting all words into a Trie, we can traverse nodes only if they mark the end of a valid word. This ensures that prefixes are valid words, helping us find the longest word built one character at a time.
Click to reveal answer
beginner
What condition must be true for a node to be considered during traversal when finding the longest word?
The node must mark the end of a word (isEnd = true) to ensure the prefix up to that node is a valid word.
Click to reveal answer
intermediate
Why do we prefer lexicographical order when multiple longest words exist?
Choosing the lexicographically smallest word ensures a consistent and predictable result when multiple words have the same maximum length.
Click to reveal answer
intermediate
What is the time complexity of building a Trie with n words of average length m?
The time complexity is O(n * m), since each character of every word is inserted once.
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.
When finding the longest word in a Trie, which nodes do we consider for traversal?
AAll nodes regardless
BOnly nodes marking the end of a word
COnly root nodes
DOnly leaf nodes
✗ Incorrect
We only traverse nodes that mark the end of a valid word to ensure prefixes are valid.
If two words have the same length, which one is chosen as the longest word?
AThe lexicographically smaller word
BThe lexicographically larger word
CThe first inserted word
DThe last inserted word
✗ Incorrect
The lexicographically smaller word is chosen for consistency.
What is the main advantage of using a Trie for prefix-based searches?
AUses less memory than arrays
BSorts words automatically
CStores numbers efficiently
DFast retrieval of words sharing prefixes
✗ Incorrect
Trie allows fast retrieval of words sharing common prefixes.
What is the time complexity to insert one word of length m into a Trie?
AO(m^2)
BO(1)
CO(m)
DO(log m)
✗ Incorrect
Inserting a word requires processing each character once, so O(m).
Explain how a Trie is used to find the longest word in a dictionary where every prefix is also a valid word.
Think about how prefixes relate to valid words in the Trie.
You got /4 concepts.
Describe the steps to build a Trie and how to traverse it to find the longest word with all prefixes valid.
Focus on insertion and traversal rules.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a Trie data structure when finding the longest word in a dictionary?
easy
A. To efficiently store and search words by their prefixes
B. To sort words alphabetically
C. To count the total number of words
D. To reverse the words quickly
Solution
Step 1: Understand Trie usage
A Trie stores words by breaking them into characters and linking nodes for prefixes.
Step 2: Connect Trie to longest word search
This structure allows quick prefix checks, which helps find the longest word built from other words.
Final Answer:
To efficiently store and search words by their prefixes -> Option A
Quick Check:
Trie = prefix search [OK]
Hint: Trie is best for prefix-based word searches [OK]
Common Mistakes:
Thinking Trie sorts words alphabetically
Confusing Trie with simple arrays
Assuming Trie counts words only
2. Which of the following TypeScript code snippets correctly initializes a Trie node with children and an end-of-word flag?
easy
A. class TrieNode { children: Map = new Map(); isEnd: boolean = false; }
B. class TrieNode { children: string[] = []; isEnd: number = 0; }
C. class TrieNode { children: object = {}; isEnd: string = 'false'; }
D. class TrieNode { children: Map = new Map(); isEnd: boolean = true; }
Solution
Step 1: Check children type
Children should map characters (string) to TrieNode objects, so Map is correct.
Step 2: Check isEnd flag type
isEnd should be boolean to mark word end, so boolean = false is correct.
Final Answer:
class TrieNode { children: Map = new Map(); isEnd: boolean = false; } -> Option A
Quick Check:
Correct types = class TrieNode { children: Map = new Map(); isEnd: boolean = false; } [OK]
Hint: Use Map and boolean for TrieNode [OK]
Common Mistakes:
Using array instead of Map for children
Using wrong types for isEnd flag
Mixing key and value types in Map
3. Given the following TypeScript code snippet that inserts words into a Trie and finds the longest word, what will be the output?
const words = ["a", "app", "apple", "apply", "ap"];
const trie = new Trie();
words.forEach(word => trie.insert(word));
console.log(trie.findLongestWord());
medium
A. "a"
B. "apply"
C. "apple"
D. "app"
Solution
Step 1: Insert words into Trie
Words "a", "app", "apple", "apply", "ap" are inserted. Trie stores prefixes for all.
Step 2: Find longest word with all prefixes present
"app" has all prefixes present: "a", "ap", "app". "apple" and "apply" require "appl" which is not a word in the list.
Final Answer:
"app" -> Option D
Quick Check:
Longest word with all prefixes = "app" [OK]
Hint: Choose longest word with all prefixes present, lex smallest if tie [OK]
Common Mistakes:
Choosing "apple" or "apply" without checking if "appl" is a word
Ignoring that intermediate prefixes like "appl" must be full words
Picking the overall longest word without prefix validation
4. Consider this buggy TypeScript code snippet for inserting words into a Trie:
insert(word: string) {
let node = this.root;
for (const char of word) {
node = node.children.get(char);
if (!node.children.has(char)) {
node.children.set(char, new TrieNode());
}
}
node.isEnd = true;
}
What is the main issue causing a runtime error?
medium
A. The loop does not iterate over all characters
B. The isEnd flag is set before inserting nodes
C. node.children.get(char) can return undefined, causing node to be undefined
D. The children map is not initialized
Solution
Step 1: Analyze node update inside loop
node.children.get(char) may return undefined if key missing, but code assumes it returns TrieNode.
Step 2: Identify cause of runtime error
If undefined assigned to node, next iteration fails accessing children of undefined.
Final Answer:
node.children.get(char) can return undefined, causing node to be undefined -> Option C
Quick Check:
Undefined node from get() causes error [OK]
Hint: Check if get() can return undefined before assignment [OK]
Common Mistakes:
Ignoring possibility of undefined from Map.get()
Assuming loop skips characters
Confusing isEnd flag placement
5. You have a list of words: ["w", "wo", "wor", "worl", "world", "banana", "ban", "bana", "banan"]. Using a Trie, which word will be the longest word in the dictionary where every prefix is also a word in the list?
hard
A. "banana"
B. "world"
C. "banan"
D. "worl"
Solution
Step 1: Check prefixes for "world"
Prefixes: "w", "wo", "wor", "worl" all exist in list.
Step 2: Check prefixes for "banana" and "banan"
"banana" and "banan" are missing shorter prefixes like "b" and "ba" which are not in the list.
Step 3: Compare valid longest words
"world" is longest with all prefixes present; others longer but invalid.
Final Answer:
"world" -> Option B
Quick Check:
Longest word with all prefixes = "world" [OK]
Hint: Check all prefixes exist; longest valid word wins [OK]
Common Mistakes:
Choosing "banana" ignoring missing prefix like "b"
Confusing similar prefixes
Not verifying all prefixes exist
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.