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 leaves represent words.
Click to reveal answer
beginner
What does the insert operation do in a Trie?
The insert operation adds a word to the Trie by creating nodes for each character if they don't exist and marking the end of the word.
Click to reveal answer
beginner
In Trie insertion, what does marking the end of a word mean?
It means setting a flag (like is_end_of_word) in the last node of the inserted word to indicate that a complete word ends there.
Click to reveal answer
intermediate
Why do we create new nodes only if the character path does not exist during insertion?
Because existing nodes represent prefixes of other words, so we reuse them to save space and avoid duplication.
Click to reveal answer
intermediate
What is the time complexity of inserting a word of length n into a Trie?
The time complexity is O(n), where n is the length of the word, because we process each character once.
Click to reveal answer
What does each node in a Trie typically represent?
AA single character
BA whole word
CA number
DA sentence
✗ Incorrect
Each node in a Trie represents a single character of a word.
During insertion, if a character node already exists, what should we do?
ACreate a new node anyway
BReuse the existing node
CDelete the existing node
DSkip the character
✗ Incorrect
We reuse existing nodes to represent shared prefixes and save space.
What flag is set to mark the end of a word in a Trie node?
Ais_word_start
Bis_leaf_node
Cis_root
Dis_end_of_word
✗ Incorrect
The is_end_of_word flag marks that a complete word ends at that node.
What is the main advantage of using a Trie for storing words?
AFaster insertion and search for prefixes
BUses less memory than arrays
CStores numbers efficiently
DAutomatically sorts words alphabetically
✗ Incorrect
Tries allow fast insertion and search especially for prefixes of words.
What is the time complexity to insert a word of length n into a Trie?
AO(log n)
BO(1)
CO(n)
DO(n^2)
✗ Incorrect
Insertion takes O(n) time because each character is processed once.
Explain step-by-step how to insert the word 'cat' into an empty Trie.
Think about creating nodes for each character and marking the last one.
You got /5 concepts.
Why is it efficient to reuse existing nodes during Trie insertion?
Consider how words with common beginnings share nodes.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the insert operation in a Trie data structure?
easy
A. To delete a word from the Trie
B. To add a new word character by character into the Trie
C. To search for a word in the Trie
D. To count the total words stored in the Trie
Solution
Step 1: Understand the insert operation
The insert operation adds each character of a word into the Trie one by one, creating nodes if they don't exist.
To add a new word character by character into the Trie -> Option B
Quick Check:
Insert = Add word stepwise [OK]
Hint: Insert means adding word letters step-by-step [OK]
Common Mistakes:
Confusing insert with search or delete
Thinking insert counts words
Assuming insert removes words
2. Which of the following is the correct way to start inserting a word "cat" into an empty Trie in Python?
easy
A. for char in "cat": current = current.children[char] if char in current.children else current.children[char] = TrieNode()
B. for char in "cat": current = current.children[char]
C. for char in "cat": current.children[char] = TrieNode(); current = current.children[char]
D. for char in "cat": if char not in current.children: current.children[char] = TrieNode(); current = current.children[char]
Solution
Step 1: Check condition before adding node
We must check if the character node exists before creating a new one to avoid overwriting.
Step 2: Move current pointer correctly
After ensuring the node exists, move current to that child node for next character insertion.
Final Answer:
for char in "cat": if char not in current.children: current.children[char] = TrieNode(); current = current.children[char] -> Option D
Quick Check:
Check existence then add and move [OK]
Hint: Check if node exists before adding new one [OK]
Common Mistakes:
Not checking if child node exists before assignment
Not moving current pointer after insertion
Overwriting existing nodes
3. Consider the following code snippet for inserting the word "dog" into a Trie. What will be the printed output after insertion?
class TrieNode:
def __init__(self):
self.children = {}
self.is_end = False
root = TrieNode()
word = "dog"
current = root
for char in word:
if char not in current.children:
current.children[char] = TrieNode()
current = current.children[char]
current.is_end = True
print(list(root.children.keys()))
medium
A. ['d']
B. ['o']
C. ['g']
D. []
Solution
Step 1: Insert characters one by one
The first character 'd' is added as a child of root, so root.children has key 'd'.
Step 2: Check what is printed
Printing root.children.keys() shows the first level keys, which is ['d'] after insertion.
Final Answer:
['d'] -> Option A
Quick Check:
Root children keys after insert = ['d'] [OK]
Hint: Root children keys show first inserted letters [OK]
Common Mistakes:
Expecting all letters at root level
Printing keys of last node instead of root
Confusing keys with values
4. The following code attempts to insert the word "bat" into a Trie but has a bug. What is the error?
class TrieNode:
def __init__(self):
self.children = {}
self.is_end = False
root = TrieNode()
word = "bat"
current = root
for char in word:
current.children[char] = TrieNode()
current = current.children[char]
current.is_end = True
medium
A. The children dictionary is not initialized
B. The is_end flag is set before inserting all characters
C. The current pointer is moved outside the loop, causing nodes to not be chained properly
D. The loop variable 'char' is not defined
Solution
Step 1: Analyze loop and pointer movement
The line moving current pointer is outside the loop, so current only moves after loop ends, not per character.
Step 2: Understand effect on insertion
Nodes for each character are created directly under root (no chaining), and only the last node's is_end is set.
Final Answer:
The current pointer is moved outside the loop, causing nodes to not be chained properly -> Option C
Quick Check:
Pointer must move inside loop to build chain [OK]
Hint: Move current pointer inside loop to link nodes [OK]
Common Mistakes:
Moving pointer outside loop
Not creating nodes conditionally
Setting is_end too early
5. You want to insert multiple words ["apple", "app", "ape"] into a Trie. After insertion, which of the following statements is TRUE about the Trie structure?
hard
A. The node for 'l' after "app" exists only for "apple"
B. The node for 'p' after 'ap' will have is_end = True for both "app" and "ape"
C. Each word creates a completely separate branch from root
D. The root node's is_end flag will be True after insertion
Solution
Step 1: Understand shared prefixes in Trie
Words "apple", "app", and "ape" share prefix "ap"; nodes for 'a' and 'p' are shared.
Step 2: Check is_end flags for words
"app" ends at second 'p' node, so that node's is_end = True; "apple" continues to 'l' and 'e', so 'l' node is not end, but 'e' node is end.
Step 3: Analyze options
The node for 'p' after 'ap' will have is_end = True for both "app" and "ape" is wrong because 'p' node cannot be end for both "app" and "ape"; 'ape' ends at 'e' node. Each word creates a completely separate branch from root is wrong because branches share prefix nodes. The root node's is_end flag will be True after insertion is wrong because root node is not end of any word.
Final Answer:
The node for 'l' after "app" exists only for "apple" -> Option A
Quick Check:
Shared prefix nodes, is_end marks word ends [OK]
Hint: Shared prefixes share nodes; is_end marks word ends only [OK]
Common Mistakes:
Assuming each word has separate root branch
Marking is_end on wrong nodes
Setting root is_end True incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.