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 is the main purpose of the insert operation in a Trie?
The insert operation adds a new word to the Trie by creating nodes for characters that do not exist and marking the end of the word.
Click to reveal answer
beginner
In the Trie insert operation, 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 true, indicating that this path forms a complete word.
Click to reveal answer
intermediate
What happens if a character of the word to insert already exists in the Trie?
The insert operation moves to the existing child node for that character without creating a new node, then continues with the next character.
Click to reveal answer
intermediate
Why is a Trie efficient for prefix-based searches?
Because common prefixes share the same path in the Trie, making it fast to find all words with a given prefix by traversing the shared nodes.
Click to reveal answer
What does each node in a Trie typically represent?
AA character of a word
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 does not exist, what should be done?
AReplace existing nodes
BSkip the character
CDelete the Trie
DCreate a new node for that character
✗ Incorrect
If the character node does not exist, a new node must be created to continue the insertion.
What flag is commonly used to mark the end of a word in a Trie node?
AisMiddleOfWord
BisStartOfWord
CisEndOfWord
DisLeafNode
✗ Incorrect
The isEndOfWord flag marks that the path to this node forms a complete word.
What is the time complexity of inserting a word of length n into a Trie?
AO(1)
BO(n)
CO(n^2)
DO(log n)
✗ Incorrect
Insertion takes O(n) time where n is the length of the word, as each character is processed once.
If two words share a prefix, how does the Trie store them?
AThey share the same nodes for the prefix characters
BThey are stored in separate Tries
CThey overwrite each other
DThey are stored as arrays
✗ Incorrect
Common prefixes share the same nodes, saving space and allowing efficient prefix searches.
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 how the Trie insert operation handles a word that shares a prefix with an existing word.
Focus on reusing existing nodes for common parts.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the insert operation in a Trie data structure?
easy
A. To add a word character by character into the Trie
B. To delete a word from the Trie
C. To search for a word in the Trie
D. To count the number of words 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.
A Trie node has an array of pointers for children (26 letters) and a boolean to mark word end.
Step 2: Match with options
typedef struct TrieNode { struct TrieNode* children[26]; bool isEndOfWord; } TrieNode; matches this structure. Others describe linked list or binary tree nodes, not Trie nodes.
Hint: Trie nodes have 26 children pointers and end-of-word flag [OK]
Common Mistakes:
Using linked list node structure instead of array
Confusing Trie with binary tree node
Missing end-of-word boolean
3. What will be the output of the following C code snippet after inserting the word "cat" into an empty Trie and checking the isEndOfWord flag at the last node?
Each character creates or uses existing nodes. The last character 't' node's isEndOfWord is set to true (1).
Step 2: Check isEndOfWord flag
Accessing the node for 't' and printing isEndOfWord prints 1 because the word ends there.
Final Answer:
1 -> Option D
Quick Check:
Inserted word end flag = 1 [OK]
Hint: End node flag is set to true after insert [OK]
Common Mistakes:
Assuming isEndOfWord is 0 after insert
Indexing children incorrectly
Forgetting to set isEndOfWord during insert
4. Consider the following buggy insert function for a Trie in C:
void insert(TrieNode* root, const char* word) {
TrieNode* node = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = word[i] - 'a';
if (node->children[index] == NULL) {
node->children[index] = malloc(sizeof(TrieNode));
}
node = node->children[index];
}
node->isEndOfWord = true;
}
What is the main bug in this code?
medium
A. New nodes are not initialized, leading to garbage values
B. The loop condition is incorrect and may cause infinite loop
C. The isEndOfWord flag is set too early
D. The root pointer is not updated after insertion
Solution
Step 1: Analyze node allocation
New nodes are allocated with malloc but not initialized, so children pointers and isEndOfWord have garbage values.
Step 2: Consequences of uninitialized nodes
Uninitialized children can cause incorrect behavior or crashes during further insert/search operations.
Final Answer:
New nodes are not initialized, leading to garbage values -> Option A
Quick Check:
Missing initialization = bug [OK]
Hint: Always initialize new Trie nodes after malloc [OK]
Common Mistakes:
Ignoring initialization of new nodes
Assuming malloc zeros memory
Confusing loop condition correctness
5. You want to insert multiple words into a Trie and then count how many words start with the prefix "pre". Which modification to the Trie insert operation helps achieve this efficiently?
hard
A. Store the entire word at each node instead of characters
B. Add a counter in each node that increments every time a word passes through it during insertion
C. Only mark isEndOfWord without any counters
D. Use a linked list instead of an array for children nodes
Solution
Step 1: Understand prefix counting
To count words with a prefix, each node can keep a count of how many words pass through it.
Step 2: Modify insert to update counters
During insertion, increment the counter at each node visited. Then prefix count is the counter at prefix's last node.
Final Answer:
Add a counter in each node that increments every time a word passes through it during insertion -> Option B
Quick Check:
Prefix count = node counters [OK]
Hint: Use counters in nodes to track prefix occurrences [OK]
Common Mistakes:
Not using counters, making prefix count slow
Storing full words at nodes wastes space
Using linked list slows access
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.