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 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 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 the algorithm do?
AReuse the existing node
BCreate a new node anyway
CDelete the existing node
DSkip the character
✗ Incorrect
The algorithm reuses existing nodes to represent shared prefixes.
What flag is commonly used to mark the end of a word in a Trie node?
AisEndOfWord
BisWordStart
CisLeafNode
DisRoot
✗ Incorrect
The isEndOfWord flag marks that a complete word ends at that node.
What is the worst-case time complexity to insert a word of length n into a Trie?
AO(1)
BO(log n)
CO(n)
DO(n^2)
✗ 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?
A0
B1
C3
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 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 one by one, creating new nodes if needed.
To add a word character by character into the Trie -> Option A
Quick Check:
Insert = Add word [OK]
Hint: Insert means adding word step-by-step [OK]
Common Mistakes:
Confusing insert with search or delete
Thinking insert counts words
Assuming insert replaces words
2. Which of the following is the correct TypeScript syntax to start inserting a word into a Trie node named root?
easy
A. let node = root;
B. node = root;
C. const node = root()
D. var node == root;
Solution
Step 1: Understand variable declaration in TypeScript
Using let declares a variable that can be reassigned, suitable for traversal nodes.
Step 2: Check syntax correctness
let node = root; uses correct assignment with let. node = root; misses declaration keyword. const node = root() wrongly calls root as a function. var node == root; uses invalid comparison operator.
Final Answer:
let node = root; -> Option A
Quick Check:
Correct variable declaration = let node = root; [OK]
Hint: Use let for variable assignment in TypeScript [OK]
Common Mistakes:
Missing let or const keyword
Using == instead of = for assignment
Calling node as a function
3. Given the following TypeScript code snippet for inserting the word "cat" into an empty Trie, what will be the printed output after insertion?
class TrieNode {
children: Map = new Map();
isEndOfWord = false;
}
const root = new TrieNode();
function insert(word: string) {
let node = root;
for (const char of word) {
if (!node.children.has(char)) {
node.children.set(char, new TrieNode());
}
node = node.children.get(char)!;
}
node.isEndOfWord = true;
}
insert("cat");
console.log(Array.from(root.children.keys()));
medium
A. ["cat"]
B. ["c", "a", "t"]
C. []
D. ["c"]
Solution
Step 1: Trace insertion of "cat" into Trie
Starting at root, 'c' is not present, so a new node for 'c' is created. Then move to 'c' node, 'a' is added as child, then 't' as child of 'a'.
Step 2: Check root's children keys
Root only has one child node for 'c'. The keys of root.children are ['c'].
Final Answer:
["c"] -> Option D
Quick Check:
Root children keys after insert = ["c"] [OK]
Hint: Root children keys show first letters only [OK]
Common Mistakes:
Expecting all letters at root level
Confusing keys with full words
Ignoring Map keys method
4. Identify the error in the following TypeScript code snippet for inserting a word into a Trie:
function insert(word: string) {
let node = root;
for (const char of word) {
if (!node.children.has(char)) {
node.children.set(char, new TrieNode());
}
node = node.children.get(char);
}
node.isEndOfWord = true;
}
medium
A. Using 'let' instead of 'const' for node
B. Missing parentheses when creating new TrieNode
C. Not checking if node.children.get(char) is undefined
D. Not marking isEndOfWord as false initially
Solution
Step 1: Check new object creation syntax
In TypeScript, when creating a new object, parentheses are required even if no arguments are passed. new TrieNode should be new TrieNode().
Step 2: Verify other parts
Using 'let' for node is fine since node changes. The get method is safe here because we just set the key if missing. isEndOfWord is set at the end, initial value doesn't cause error.
Final Answer:
Missing parentheses when creating new TrieNode -> Option B
Quick Check:
new object needs () [OK]
Hint: Always use () when calling constructors [OK]
Common Mistakes:
Omitting () after new keyword
Confusing let and const for variables
Ignoring possible undefined from get()
5. You want to insert multiple words ["bat", "ball", "batman"] into a Trie. After inserting all, which of the following statements is true about the Trie structure?
hard
A. The node for 'b' has children 'a' and 'm' directly
B. Each word creates a separate branch from root
C. The node for 'b' has children 'a' only, and 'a' has children 't' and 'l'
D. The node for 't' is the end of word for both "bat" and "batman"
Solution
Step 1: Analyze common prefixes in words
Words "bat", "ball", and "batman" share prefix 'b' -> 'a'. From 'a', 't' leads to "bat" and "batman", 'l' leads to "ball".
Step 2: Understand Trie branching
Node 'b' has child 'a'. Node 'a' branches to 't' and 'l'. 'm' is child of 'a'->'t'->'m' for "batman". So 'm' is not direct child of 'b'.
Final Answer:
The node for 'b' has children 'a' only, and 'a' has children 't' and 'l' -> Option C
Quick Check:
Trie branches at shared prefixes [OK]
Hint: Words share nodes for common prefixes only [OK]
Common Mistakes:
Assuming all words start new branches at root
Thinking 'm' is direct child of 'b'
Confusing end of word nodes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.