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 characters of the word. It can quickly check if a word or prefix exists by following the path in the Trie.
Click to reveal answer
beginner
What does the 'end of word' marker in a Trie node signify?
It marks that the path from the root to this node forms a complete valid word stored in the Trie.
Click to reveal answer
beginner
In a Trie, what happens if a character is not found during search?
If a character is missing in the current node's children during search, it means the word or prefix does not exist in the Trie.
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, avoiding repeated comparisons, and enabling search in O(m) time where m is the word length, unlike list search which can be O(n*m).
Click to reveal answer
What does each node in a Trie represent?
AA whole word
BA number
CA character of a word
DA sentence
✗ Incorrect
Each node in a Trie represents a single character of a word.
What indicates that a node marks the end of a valid word in a Trie?
AA special end-of-word flag
BThe node has no children
CThe node is the root
DThe node has multiple children
✗ Incorrect
A special flag or marker in the node indicates the end of a valid word.
If a character is missing in the Trie during search, what does it mean?
AThe search should continue
BThe word or prefix does not exist
CThe Trie is empty
DThe word exists
✗ Incorrect
Missing character means the word or prefix is not stored in the Trie.
What is the time complexity of searching a word of length m in a Trie?
AO(1)
BO(n)
CO(m*n)
DO(m)
✗ Incorrect
Searching 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?
AFast prefix search
BUses less memory than arrays
CStores numbers efficiently
DSorts words automatically
✗ Incorrect
Trie allows fast prefix-based search by traversing character nodes.
Explain how a Trie is used to search for a word step-by-step.
Think about following the path of characters in the Trie.
You got /4 concepts.
Describe the advantages of using a Trie over a list for storing and searching words.
Consider how words with same beginnings are stored.
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 store words in a way that allows fast prefix-based search
B. To sort words alphabetically
C. To store words in a linked list
D. To compress words into a single string
Solution
Step 1: Understand the Trie structure
A Trie stores characters of words in a tree-like structure where each node represents a character.
Step 2: Identify the main use in word search
This structure allows quick lookup of words by their prefixes, making prefix-based search very efficient.
Final Answer:
To store words in a way that allows fast prefix-based search -> Option A
Quick Check:
Trie = fast prefix search [OK]
Hint: Trie is all about prefix search speed [OK]
Common Mistakes:
Confusing Trie with sorting structures
Thinking Trie stores words as linked lists
Assuming Trie compresses words into strings
2. Which of the following is the correct way to check if a character ch exists as a child node in a Trie node's children dictionary named children?
easy
A. if ch in children.keys():
B. if children.has_key(ch):
C. if ch in children:
D. if children.contains(ch):
Solution
Step 1: Recall Python dictionary membership check
In Python, to check if a key exists in a dictionary, use key in dict.
Step 2: Identify the correct syntax
if ch in children: is the correct and most Pythonic way to check key existence.
Final Answer:
if ch in children: -> Option C
Quick Check:
Python dict key check = 'in' keyword [OK]
Hint: Use 'in' keyword to check dict keys in Python [OK]
Common Mistakes:
Using deprecated has_key() method
Checking keys() explicitly which is slower
Using non-existent contains() method
3. Consider the following Python code snippet for searching a word in a Trie:
def search_word(root, word):
node = root
for ch in word:
if ch not in node.children:
return False
node = node.children[ch]
return node.is_end_of_word
# Trie nodes and root are properly initialized
result = search_word(root, 'cat')
If the Trie contains the words ['car', 'cat', 'cap'], what will be the value of result?
medium
A. None
B. False
C. KeyError
D. True
Solution
Step 1: Trace the search for 'cat'
The function checks each character 'c', 'a', 't' in the Trie nodes. Since 'cat' is inserted, all characters exist and the last node marks end of word.
Step 2: Return the end of word flag
Because 'cat' is a complete word in the Trie, node.is_end_of_word is True, so the function returns True.
Final Answer:
True -> Option D
Quick Check:
Word 'cat' found = True [OK]
Hint: If all chars found and end flag True, return True [OK]
Common Mistakes:
Returning True without checking end_of_word flag
Assuming partial prefix means word exists
Confusing missing character with KeyError
4. The following code tries to search a word in a Trie but raises an error. Identify the bug:
def search_word(root, word):
node = root
for ch in word:
if ch not in node:
return False
node = node[ch]
return node.is_end_of_word
medium
A. Should use 'node.children[ch]' instead of 'node[ch]'
B. Both B and C are correct
C. Should check 'ch not in node.children' instead of 'ch not in node'
D. No bug, code is correct
Solution
Step 1: Analyze membership check
The code checks if ch not in node, but node is an object, not a dictionary. It should check node.children.
Step 2: Analyze node traversal
Similarly, node = node[ch] is incorrect; it should be node = node.children[ch] to access the child node.
Final Answer:
Both B and C are correct -> Option B
Quick Check:
Access children dict properly = Both fixes needed [OK]
Hint: Always access children dict, not node directly [OK]
Common Mistakes:
Checking keys on node object instead of children dict
Assigning node = node[ch] instead of node.children[ch]
Ignoring attribute errors from wrong access
5. You want to find if any word in a Trie starts with a given prefix prefix. Which modification to the standard word search function below will correctly check for prefix existence?
def starts_with(root, prefix):
node = root
for ch in prefix:
if ch not in node.children:
return False
node = node.children[ch]
# What to return here?
hard
A. Return True immediately after loop ends
B. Return node.is_end_of_word
C. Return False if node has no children
D. Return True only if prefix length is 1
Solution
Step 1: Understand prefix search logic
To check if any word starts with the prefix, we only need to confirm all prefix characters exist in the Trie.
Step 2: Decide return value after loop
If all characters are found, return True immediately. We do not need to check if the node marks end of a word.
Final Answer:
Return True immediately after loop ends -> Option A
Quick Check:
Prefix found = True [OK]
Hint: If prefix chars found, return True regardless of word end [OK]
Common Mistakes:
Returning is_end_of_word which fails for prefixes not full words
Checking children existence unnecessarily
Restricting prefix length to 1
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.