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 down the tree 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 isEndOfWord) 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 character
BA whole word
CAn integer value
DA number of words
✗ 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
BDelete the existing node
CReuse the existing node
DSkip the character
✗ Incorrect
We reuse existing nodes to represent shared prefixes and save space.
What flag is commonly used to mark the end of a word in a Trie node?
AisWordEnd
BisEndOfWord
CwordComplete
DendFlag
✗ Incorrect
The flag is commonly named isEndOfWord to indicate the end of a word.
What is the worst-case time complexity to insert a word of length n in a Trie?
AO(n)
BO(log n)
CO(n^2)
DO(1)
✗ Incorrect
Insertion requires processing each character once, so O(n).
If you insert the words 'cat' and 'car' into a Trie, how many nodes will be shared?
A3
B1
C0
D2
✗ Incorrect
The nodes for 'c' and 'a' are shared, so 2 nodes.
Explain step-by-step how to insert the word 'dog' into an empty Trie.
Think about creating nodes for each character and marking the last one.
You got /5 concepts.
Describe why Tries are efficient for prefix-based searches and how insertion supports this.
Focus on shared nodes and marking word ends.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of the insert operation in a Trie data structure?
easy
A. To count the number of words stored in the Trie
B. To delete a word from the Trie
C. To add a new word character by character into the Trie
D. To search for a word in the Trie
Solution
Step 1: Understand the insert operation
The insert operation adds each character of a word into the Trie, creating nodes if they don't exist.
Hint: Children must be pointers, initialized to nullptr [OK]
Common Mistakes:
Using int array instead of pointers
Not initializing children to nullptr
Using vector<char> instead of TrieNode pointers
3. What will be the output of the following code snippet after inserting the words "cat" and "car" into an empty Trie?
Trie trie;
trie.insert("cat");
trie.insert("car");
int count = 0;
for(int i = 0; i < 26; i++) {
if(trie.root->children[i] != nullptr) count++;
}
std::cout << count << std::endl;
medium
A. 1
B. 2
C. 3
D. 0
Solution
Step 1: Insert "cat" and "car" into Trie
Both words start with 'c', so only one child node for 'c' is created at root.
Step 2: Count root children nodes
Only one child at root for 'c' exists, so count is 1.
Final Answer:
1 -> Option A
Quick Check:
Root children count after insert = 1 [OK]
Hint: Count root children after insert; common prefix shares node [OK]
Common Mistakes:
Counting all characters instead of root children
Assuming separate root nodes for each word
Confusing total nodes with root children
4. Identify the error in the following Trie insert function snippet:
void insert(string word) {
TrieNode* node = root;
for(char c : word) {
int index = c - 'a';
if(node->children[index] == NULL) {
node->children[index] = new TrieNode();
}
node = node->children[index];
}
node->isEnd = true;
}
medium
A. No error; the insert function is correct
B. The index calculation should be c - 'A' instead of c - 'a'
C. The root pointer is not initialized before insertion
D. The loop should iterate over indices, not characters
Solution
Step 1: Check root initialization assumption
Assuming root is properly initialized elsewhere, this code correctly uses root pointer.
Step 2: Verify index calculation and loop
Index c - 'a' is correct for lowercase letters; loop over characters is valid.
Final Answer:
No error; the insert function is correct -> Option A
Quick Check:
Code logic matches standard insert [OK]
Hint: Check index and pointer usage carefully [OK]
Common Mistakes:
Assuming uppercase letters without context
Thinking loop must use indices
Forgetting root initialization outside snippet
5. Given a Trie that already contains the words "bat", "ball", and "barn", what will be the effect of inserting the word "bar"? Choose the correct statement.
hard
A. A new branch is created starting from 'b' node
B. No new nodes are created; only an existing node's isEnd flag is set
C. All existing nodes are duplicated to insert the new word
D. The word "bar" cannot be inserted because "barn" exists
Solution
Step 1: Analyze existing Trie structure
Words "bat", "ball", "barn" share prefix 'ba' and 'bar' prefix exists as part of "barn".
Step 2: Effect of inserting "bar"
Nodes for 'b', 'a', 'r' already exist; only isEnd flag at 'r' node is set to true to mark "bar" as a word.
Final Answer:
No new nodes are created; only an existing node's isEnd flag is set -> Option B
Quick Check:
Insert existing prefix sets isEnd only [OK]
Hint: Insert existing prefix sets isEnd flag only [OK]
Common Mistakes:
Thinking new nodes are always created
Assuming duplicates of nodes happen
Believing insertion fails if prefix exists
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.