A Trie is a tree-like data structure used to store a dynamic set of strings, where each node represents a character. It allows fast retrieval of words by prefix.
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 show 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 previously inserted words, so we reuse them to save space and time.
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 whole word
BA single character
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 you 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 set to mark the end of a word in a Trie node?
AisEndOfWord
BisRoot
CisLeafNode
DisWordStart
✗ Incorrect
The isEndOfWord flag marks that a complete word ends at that node.
What is the best description of the insert operation's time complexity in a Trie?
AO(n) where n is word length
BO(1)
CO(n^2)
DO(log n)
✗ Incorrect
Insertion takes O(n) time because each character of the word is processed once.
Which of these is NOT a benefit of using a Trie for insertion?
AFast prefix search
BEfficient memory for shared prefixes
CSorting words automatically
DConstant time insertion regardless of word length
✗ Incorrect
Insertion time depends on word length, so it is not constant time.
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.
Describe why Tries are efficient for storing many words with common prefixes.
Consider how nodes represent characters and how prefixes overlap.
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 count the number of words stored in the Trie
B. To delete a word from the Trie
C. To search for a word in the Trie
D. To add a word character by character into 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 word character by character into the Trie -> Option D
Quick Check:
Insert = Add word step-by-step [OK]
Hint: Insert means adding word letters one by one [OK]
Common Mistakes:
Confusing Insert with Delete or Search
Thinking Insert counts words
Assuming Insert removes nodes
2. Which of the following is the correct way to declare a Trie node class in C# for insertion?
easy
A. public class TrieNode { public Dictionary Children = new Dictionary(); public bool IsEndOfWord = false; }
B. public class TrieNode { public List<int> Children; public bool IsEndOfWord; }
C. public class TrieNode { public int[] Children = new int[26]; public bool IsEndOfWord; }
D. public class TrieNode { public string Children; public bool IsEndOfWord; }
Solution
Step 1: Identify correct data structure for children
Trie nodes use a dictionary mapping characters to child nodes for flexible branching.
Step 2: Check class members
public class TrieNode { public Dictionary Children = new Dictionary(); public bool IsEndOfWord = false; } uses Dictionary and a boolean flag for word end, which is standard.
Final Answer:
public class TrieNode { public Dictionary Children = new Dictionary(); public bool IsEndOfWord = false; } -> Option A
Quick Check:
Children dictionary + end flag = public class TrieNode { public Dictionary Children = new Dictionary(); public bool IsEndOfWord = false; } [OK]
Hint: Use Dictionary for children in C# Trie nodes [OK]
Common Mistakes:
Using List<int> instead of dictionary for children
Using int array without node references
Using string instead of node references
3. Consider the following C# code inserting the word "cat" into an empty Trie. What will be the state of the root node's children after insertion?
Trie trie = new Trie();
trie.Insert("cat");
var childrenKeys = trie.Root.Children.Keys.ToList();
childrenKeys.Sort();
Console.WriteLine(string.Join(",", childrenKeys));
medium
A. "t"
B. "a"
C. "c"
D. "cat"
Solution
Step 1: Insert "cat" character by character
First character 'c' is added as a child of root node.
Step 2: Check root's children keys
Only 'c' is directly under root; 'a' and 't' are children of subsequent nodes.
Final Answer:
"c" -> Option C
Quick Check:
Root children keys = first letter only [OK]
Hint: Root children keys = first letters of inserted words [OK]
Common Mistakes:
Assuming all letters appear at root children
Confusing nested children with root children
Printing full word as a key
4. The following C# code attempts to insert the word "dog" into a Trie but has a bug. What is the error?
public void Insert(string word) {
var node = Root;
foreach (char c in word) {
if (!node.Children.ContainsKey(c)) {
node.Children[c] = new TrieNode();
}
node = node.Children[c];
}
node.IsEndOfWord = false;
}
medium
A. The IsEndOfWord flag should be set to true after insertion
B. The foreach loop should iterate backwards
C. Children dictionary should be cleared before insertion
D. Root node should be reinitialized inside the method
Solution
Step 1: Analyze IsEndOfWord assignment
After inserting all characters, IsEndOfWord marks the end of a valid word and must be true.
Step 2: Identify the bug
Code sets IsEndOfWord to false, which incorrectly marks the word as not complete.
Final Answer:
The IsEndOfWord flag should be set to true after insertion -> Option A
Quick Check:
End flag = true after insert [OK]
Hint: Set IsEndOfWord = true after inserting full word [OK]
Common Mistakes:
Setting IsEndOfWord to false instead of true
Reinitializing root unnecessarily
Clearing children during insert
5. You want to insert multiple words into a Trie and then check if the word "bat" exists. Which sequence of operations correctly inserts "bat", "ball", and "batman" and marks them properly in C#?
hard
A. Insert "bat", Insert "ball", Insert "batman"; set IsEndOfWord true at root node
B. Insert "bat", Insert "ball", Insert "batman"; set IsEndOfWord true only at last character of each word
C. Insert "bat", Insert "ball", Insert "batman"; set IsEndOfWord true for all nodes
D. Insert "bat", Insert "ball", Insert "batman"; set IsEndOfWord false for all nodes
Solution
Step 1: Insert each word fully
Insert each word character by character, creating nodes as needed.
Step 2: Mark end of each word
Set IsEndOfWord = true only at the last character node of each inserted word to mark valid words.
Final Answer:
Insert "bat", Insert "ball", Insert "batman"; set IsEndOfWord true only at last character of each word -> Option B
Quick Check:
End flag true only at word ends [OK]
Hint: Mark end flag true only at last letter of each word [OK]
Common Mistakes:
Marking root or all nodes as end of word
Not marking end of word nodes
Setting end flag false after insert
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.