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 the root to leaves represent words.
Click to reveal answer
beginner
How does a Trie help in word search?
A Trie allows fast word search by traversing nodes character by character. If the path for all characters of a word exists and ends at a node marked as a word end, the word is found.
Click to reveal answer
beginner
What does the 'isEndOfWord' flag represent in a Trie node?
It marks whether the path from the root to this node forms a complete word stored in the Trie.
Click to reveal answer
intermediate
In C#, how do you typically represent children nodes in a Trie?
Children nodes are often stored in an array or dictionary indexed by characters, allowing quick access to the next node for a given character.
Click to reveal answer
intermediate
What is the time complexity of searching a word in a Trie?
The time complexity is O(m), where m is the length of the word, because each character is checked once by traversing the Trie nodes.
Click to reveal answer
What does each node in a Trie represent?
AA character of a word
BA whole word
CAn integer value
DA pointer to the next word
✗ Incorrect
Each node in a Trie represents a single character of a word.
How do you know a word ends in a Trie?
AWhen the node has exactly one child
BWhen the node has no children
CWhen the node is the root
DWhen the node's isEndOfWord flag is true
✗ Incorrect
The isEndOfWord flag marks the end of a complete word in the Trie.
What is the best data structure to store children nodes in a Trie for English lowercase letters?
AQueue
BArray of size 26
CStack
DLinked list
✗ Incorrect
An array of size 26 allows direct access to each lowercase English letter child node.
What is the time complexity to search a word of length m in a Trie?
AO(m)
BO(1)
CO(log m)
DO(m^2)
✗ Incorrect
Searching takes O(m) time because each character is checked once.
If a word is not in the Trie, what happens during search?
ASearch returns true anyway
BSearch loops infinitely
CSearch stops when a character node is missing
DSearch returns the closest word
✗ Incorrect
Search stops as soon as a character node is missing, meaning the word is not present.
Explain how to search for a word in a Trie step-by-step.
Think about walking down the tree following characters.
You got /6 concepts.
Describe the structure of a Trie node used for word search.
Focus on what each node stores and how it connects.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a Trie data structure in word search problems?
easy
A. To efficiently store and search words by their prefixes
B. To sort words alphabetically
C. To store words in a linked list
D. To perform arithmetic operations on words
Solution
Step 1: Understand Trie structure
A Trie stores characters of words in a tree-like structure where each node represents a character.
Step 2: Identify Trie usage in word search
It allows quick prefix-based search, making it efficient to find words starting with given letters.
Final Answer:
To efficiently store and search words by their prefixes -> Option A
Quick Check:
Trie = prefix search [OK]
Hint: Trie is best for prefix-based word searches [OK]
Common Mistakes:
Confusing Trie with sorting structures
Thinking Trie stores words linearly
Assuming Trie is for arithmetic
2. Which of the following is the correct way to declare a Trie node class in C#?
easy
A. public class TrieNode { public Dictionary Children = new Dictionary(); public bool IsWordEnd = false; }
B. public class TrieNode { public List<int> Children; public bool IsWordEnd; }
C. public class TrieNode { public string Children; public int IsWordEnd; }
D. public class TrieNode { public int Children; public bool IsWordEnd; }
Solution
Step 1: Check data types for children
Trie children should map characters to TrieNode objects, so Dictionary is correct.
Step 2: Check word end marker
Boolean flag IsWordEnd indicates if node ends a word, so bool is correct type.
Final Answer:
public class TrieNode { public Dictionary Children = new Dictionary(); public bool IsWordEnd = false; } -> Option A
Quick Check:
Children dictionary + bool flag = public class TrieNode { public Dictionary Children = new Dictionary(); public bool IsWordEnd = false; } [OK]
Hint: Trie node children map chars to nodes with bool end flag [OK]
Common Mistakes:
Using List<int> instead of Dictionary
Using string or int for children
Using int instead of bool for IsWordEnd
3. Given the following C# code snippet for searching a word in a Trie, what will be the output?
Trie trie = new Trie();
trie.Insert("cat");
trie.Insert("car");
trie.Insert("dog");
bool result = trie.Search("cap");
Console.WriteLine(result);
medium
A. True
B. False
C. Compilation error
D. Runtime exception
Solution
Step 1: Understand inserted words
Words "cat", "car", and "dog" are inserted into the Trie.
Step 2: Search for "cap"
"cap" shares prefix "ca" but 'p' is not inserted after 'a', so search returns false.
Final Answer:
False -> Option B
Quick Check:
Word not found = False [OK]
Hint: Search returns false if full word path missing [OK]
Common Mistakes:
Assuming prefix match means word found
Expecting true for partial matches
Confusing compilation/runtime errors
4. Identify the error in the following C# Trie search method:
public bool Search(string word) {
TrieNode node = root;
foreach (char c in word) {
if (!node.Children.ContainsKey(c))
return false;
node = node.Children[c];
}
return true;
}
medium
A. It does not check for null root
B. It throws NullReferenceException if word is empty
C. It returns true even if the word is only a prefix, not a complete word
D. It uses wrong data type for node
Solution
Step 1: Analyze search logic
The method checks if all characters exist in Trie nodes but does not check if last node marks end of a word.
Step 2: Identify missing condition
Without checking node.IsWordEnd, it returns true for prefixes that are not complete words.
Final Answer:
It returns true even if the word is only a prefix, not a complete word -> Option C
Quick Check:
Missing IsWordEnd check = It returns true even if the word is only a prefix, not a complete word [OK]
Hint: Always check IsWordEnd after traversal [OK]
Common Mistakes:
Ignoring IsWordEnd flag
Assuming prefix means word found
Not handling empty strings
5. You want to find all words in a Trie that start with the prefix "pre". Which approach correctly uses the Trie to achieve this?
hard
A. Use a hash set to store all words and filter those starting with "pre"
B. Search each word in the Trie individually to check if it starts with "pre"
C. Insert all words starting with "pre" into a new Trie and search there
D. Traverse the Trie to the node representing 'e' in "pre", then perform a DFS to collect all words below
Solution
Step 1: Locate prefix node in Trie
Traverse characters 'p', 'r', 'e' to reach the node representing last prefix character.
Step 2: Collect words using DFS
From this node, perform depth-first search to find all complete words that start with "pre".
Final Answer:
Traverse the Trie to the node representing 'e' in "pre", then perform a DFS to collect all words below -> Option D
Quick Check:
Prefix node + DFS = Traverse the Trie to the node representing 'e' in "pre", then perform a DFS to collect all words below [OK]
Hint: Find prefix node, then DFS for words [OK]
Common Mistakes:
Checking all words individually
Using unrelated data structures
Rebuilding Trie unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.