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) on 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 does not exist in the current Trie node during insertion?
Because existing nodes represent prefixes already stored, so we reuse them to save space and only add new nodes for new characters.
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 sentence
BA character of a word
CA number
DA whole word
✗ Incorrect
Each node in a Trie represents a single character of a word.
During insertion, if a character node already exists, what should the algorithm do?
AReuse the existing node and move to it
BCreate a new node anyway
CDelete the existing node
DStop insertion
✗ Incorrect
If the node exists, reuse it to continue inserting the next character.
What flag is commonly used to mark the end of a word in a Trie node?
AisWordStart
BisLeafNode
CisEndOfWord
DisRoot
✗ Incorrect
The isEndOfWord flag marks the node as the end of a valid word.
What is the main advantage of using a Trie for storing words?
AStores numbers efficiently
BUses less memory than arrays
CSorts words automatically
DFaster insertion and search by prefix
✗ Incorrect
Tries allow fast insertion and search especially for prefix-based queries.
If you insert the word 'cat' and then 'car', how many new nodes are created for 'car'?
A1
B2
C3
D0
✗ Incorrect
Only the last character 'r' node is new; 'c' and 'a' nodes already exist from 'cat'.
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 searches and how insertion supports this.
Focus on shared prefixes and node reuse.
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 total 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 new 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.
Step 2: Compare with other operations
Deleting removes words, searching finds words, and counting totals words, but insert specifically adds words.
Final Answer:
To add a new word character by character into the Trie -> Option D
Quick Check:
Insert = Add word step-by-step [OK]
Hint: Insert means adding word letters stepwise [OK]
Common Mistakes:
Confusing insert with search or delete
Thinking insert adds whole word as one node
Assuming insert counts words
2. Which of the following is the correct way to start inserting a word "cat" into an empty Trie in JavaScript?
easy
A. for (let char of word) { if (!node.children[char]) node.children[char] = new TrieNode(); node = node.children[char]; }
B. for (let i = 0; i < word.length; i++) { node = node.children[word[i]]; }
C. while (word) { node.children[word[0]] = new TrieNode(); word = word.slice(1); }
D. node.children[word] = new TrieNode();
Solution
Step 1: Analyze the loop for insertion
for (let char of word) { if (!node.children[char]) node.children[char] = new TrieNode(); node = node.children[char]; } loops over each character, checks if child node exists, creates if not, then moves to that child node.
Step 2: Check other options for correctness
for (let i = 0; i < word.length; i++) { node = node.children[word[i]]; } does not create nodes if missing. while (word) { node.children[word[0]] = new TrieNode(); word = word.slice(1); } modifies the word string but does not move node properly. node.children[word] = new TrieNode(); tries to add whole word as one child.
Final Answer:
for (let char of word) { if (!node.children[char]) node.children[char] = new TrieNode(); node = node.children[char]; } -> Option A
3. What will be the structure of the Trie after inserting the words ["bat", "ball"] in order? Consider each node stores children in an object and a boolean isEnd for word end.
medium
A. {"b":{"a":{"t":{"isEnd":false},"l":{"l":{"isEnd":true}}}}}
B. {"b":{"a":{"t":{"isEnd":true},"l":{"isEnd":true}}}}
C. {"b":{"a":{"t":{"isEnd":true},"l":{"l":{"isEnd":true}}}}}
D. {"b":{"a":{"t":{"isEnd":true}},"l":{"l":{"isEnd":true}}}}
Solution
Step 1: Insert "bat" into empty Trie
Creates nodes b -> a -> t with t's isEnd true.
Step 2: Insert "ball" next
Nodes b -> a exist, then add l -> l with last l's isEnd true.
Final Answer:
{"b":{"a":{"t":{"isEnd":true},"l":{"l":{"isEnd":true}}}}} -> Option C
Quick Check:
Both words share prefix 'ba', separate branches for 't' and 'll' [OK]
Hint: Shared prefix nodes, separate branches for different suffixes [OK]
Common Mistakes:
Marking wrong nodes as word end
Not creating second 'l' node for 'ball'
Overwriting nodes incorrectly
4. Given this insert function snippet, what is the bug?
insert(word) {
let node = this.root;
for (let char of word) {
if (!node.children[char]) {
node.children[char] = new TrieNode();
}
node = node.children;
}
node.isEnd = true;
}
medium
A. Missing check for empty word input
B. node is assigned to node.children instead of node.children[char]
C. isEnd should be set before the loop
D. Children should be an array, not an object
Solution
Step 1: Check node update inside loop
Inside loop, node is set to node.children (object), not node.children[char] (child node), so traversal breaks.
Step 2: Confirm correct pointer update
Correct is node = node.children[char] to move down the Trie properly.
Final Answer:
node is assigned to node.children instead of node.children[char] -> Option B
Quick Check:
Update node to child node, not children object [OK]
Hint: Update node to child node, not children object [OK]
Common Mistakes:
Assigning node to children object instead of child node
Setting isEnd too early
Ignoring empty input check
5. You want to insert the words ["apple", "app", "ape"] into a Trie and then check if the word "app" is a complete word in the Trie. Which approach correctly handles this scenario?
hard
A. Insert each word character by character, mark isEnd true at last character; then search for "app" checking isEnd at last node
B. Insert all words, but mark isEnd true only for the longest word; search ignores isEnd
C. Insert words as full strings in one node; search by matching full string keys
D. Insert words without marking isEnd; search by checking if path exists
Solution
Step 1: Insert words with isEnd marking
Each word is inserted letter by letter; isEnd is set true at last letter node to mark complete words.
Step 2: Search for "app" with isEnd check
Search traverses nodes for 'a'->'p'->'p' and checks isEnd true to confirm "app" is a complete word, not just prefix.
Final Answer:
Insert each word character by character, mark isEnd true at last character; then search for "app" checking isEnd at last node -> Option A
Quick Check:
Mark word ends, check isEnd to confirm full word [OK]
Hint: Mark word ends with isEnd, check isEnd when searching [OK]
Common Mistakes:
Not marking isEnd for shorter words
Treating prefixes as complete words without isEnd
Storing whole words as single nodes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.