A Trie is a tree-like data structure used to store a dynamic set of strings where keys are usually strings. It allows fast retrieval of words by storing characters in nodes along paths.
Click to reveal answer
beginner
How does insertion work in a Trie?
Insertion adds a word by creating nodes for each character if they don't exist, following the path from the root. The last node is marked to show the end of a word.
Click to reveal answer
beginner
What does searching in a Trie involve?
Searching checks each character of the word by moving through nodes. If all characters are found and the last node is marked as a word end, the word exists in the Trie.
Click to reveal answer
intermediate
Why is Trie efficient for prefix searches?
Because nodes represent characters in order, all words sharing a prefix share the same path. This makes finding all words with a prefix very fast.
Click to reveal answer
intermediate
What is the time complexity of inserting or searching a word in a Trie?
Both insertion and search take time proportional to the length of the word, usually O(m), where m is the number of characters in the word.
Click to reveal answer
What does each node in a Trie typically represent?
AA sentence
BA whole word
CA number
DA character of a word
✗ Incorrect
Each node in a Trie represents a single character of a word.
When inserting a word into a Trie, what happens if a character node already exists?
ASkip to the next character node
BDelete the existing node
CCreate a new node anyway
DMark the node as a word end immediately
✗ Incorrect
If the character node exists, insertion moves to that node to continue with the next character.
How do you know a word ends in a Trie node?
AThe node is the root
BThe node has no children
CThe node has a special marker or flag
DThe node stores the word length
✗ Incorrect
A node is marked with a flag to indicate the end of a valid word.
What is the main advantage of using a Trie over a list for word search?
AUses less memory
BFaster search by prefix
CSimpler to implement
DStores numbers better
✗ Incorrect
Tries allow very fast prefix searches compared to lists.
What is the worst-case time complexity to search a word of length m in a Trie?
AO(m)
BO(m^2)
CO(log m)
DO(1)
✗ Incorrect
Searching takes time proportional to the length of the word, O(m).
Explain how to insert a word into a Trie step-by-step.
Think about walking through the word's letters and building the path.
You got /5 concepts.
Describe how searching for a word in a Trie works and how you know if the word exists.
Imagine tracing the word letter by letter in the Trie.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Trie data structure in storing words?
easy
A. To store words letter by letter in a tree for fast prefix search
B. To store words as a single string in a list
C. To store words in random order for quick access
D. To store words using hash codes only
Solution
Step 1: Understand Trie structure
A Trie stores words by breaking them into letters and linking them in a tree structure.
Step 2: Identify purpose
This structure allows fast searching of words by prefixes, unlike storing whole words as strings or hashes.
Final Answer:
To store words letter by letter in a tree for fast prefix search -> Option A
Quick Check:
Trie = letter-by-letter tree [OK]
Hint: Trie stores words letter by letter for prefix search [OK]
Common Mistakes:
Thinking Trie stores whole words as strings
Confusing Trie with hash tables
Assuming Trie stores words randomly
2. Which of the following correctly describes the insertion process in a Trie?
easy
A. Replace existing nodes with new letters
B. Add the whole word as a single node
C. Insert letters only if the word is not already present
D. Add nodes for each letter if missing and mark the last node as end of word
Solution
Step 1: Review insertion steps
Insertion checks each letter; if a letter node is missing, it creates it.
Step 2: Mark end of word
After all letters are inserted, the last node is marked to show the word ends there.
Final Answer:
Add nodes for each letter if missing and mark the last node as end of word -> Option D
Quick Check:
Insert = add letters + mark end [OK]
Hint: Insert letter nodes if missing, mark last as word end [OK]
Common Mistakes:
Inserting whole word as one node
Not marking the end of word
Replacing existing nodes unnecessarily
3. Given a Trie with words: cat, car, and dog, what will be the result of searching for cap?
medium
A. True, because 'cap' is similar to 'car'
B. True, because 'ca' prefix exists
C. False, because 'p' node does not exist after 'ca'
D. Error, search cannot handle missing nodes
Solution
Step 1: Check prefix nodes
The Trie has nodes for 'c' and 'a' from 'cat' and 'car'.
Step 2: Verify next letter node
There is no 'p' node after 'a', so 'cap' is not found.
Final Answer:
False, because 'p' node does not exist after 'ca' -> Option C
Quick Check:
Missing letter node means word not found [OK]
Hint: Search fails if any letter node missing [OK]
Common Mistakes:
Assuming prefix presence means full word found
Confusing similar words as found
Expecting errors instead of false
4. Identify the error in this Trie insertion pseudocode:
for letter in word:
if letter in current_node:
current_node = current_node[letter]
else:
current_node[letter] = {}
current_node['end'] = True
medium
A. The 'end' marker is set outside the loop, should be inside
B. The else block should update current_node after adding new node
C. The loop should iterate over indices, not letters
D. The code incorrectly uses a dictionary for nodes
Solution
Step 1: Analyze insertion logic
When a new letter node is created, the code does not move current_node to this new node.
Step 2: Identify fix
After adding a new node, current_node must be updated to that node to continue insertion correctly.
Final Answer:
The else block should update current_node after adding new node -> Option B
Quick Check:
Update current_node after adding new letter node [OK]
Hint: Move current_node after adding new node [OK]
Common Mistakes:
Not updating current_node after insertion
Setting 'end' marker inside loop incorrectly
Confusing iteration over letters vs indices
5. You want to insert the words bat, bath, and baton into a Trie. How many nodes will the Trie have after all insertions?
hard
A. 9 nodes
B. 8 nodes
C. 7 nodes
D. 10 nodes
Solution
Step 1: Insert 'bat'
Creates nodes: 'b', 'a', 't' -> 3 nodes (root is not counted as a node).
Step 2: Insert 'bath'
'b', 'a', 't' exist; add 'h' -> 1 new node; total 4 nodes.
Step 3: Insert 'baton'
'b', 'a', 't' exist; add 'o', 'n' -> 2 new nodes; total 6 nodes.
Step 4: Count 'end' markers as nodes
Each word end is marked, adding 3 'end' markers as nodes; total nodes = 6 + 3 = 9 nodes.
Step 5: Confirm total nodes
Nodes: b, a, t, h, o, n plus 3 'end' markers = 9 nodes.
Final Answer:
9 nodes -> Option A
Quick Check:
Letter nodes + end markers = 9 [OK]
Hint: Count letter nodes plus 'end' markers as nodes = 9 [OK]
Common Mistakes:
Counting duplicate nodes multiple times
Ignoring shared prefixes
Miscounting letters in longer words
Not counting 'end' markers as nodes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.