Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting words into a Trie?
Consider a Trie where we insert the words "cat", "car", and "dog" in this order. What is the printed structure of the Trie after these insertions?
DSA C#
public class TrieNode { public Dictionary<char, TrieNode> Children = new Dictionary<char, TrieNode>(); public bool IsEndOfWord = false; } public class Trie { private TrieNode root = new TrieNode(); public void Insert(string word) { TrieNode node = root; foreach (char c in word) { if (!node.Children.ContainsKey(c)) { node.Children[c] = new TrieNode(); } node = node.Children[c]; } node.IsEndOfWord = true; } public void PrintTrie() { PrintNode(root, ""); } private void PrintNode(TrieNode node, string prefix) { if (node.IsEndOfWord) { Console.WriteLine(prefix); } foreach (var child in node.Children) { PrintNode(child.Value, prefix + child.Key); } } } var trie = new Trie(); trie.Insert("cat"); trie.Insert("car"); trie.Insert("dog"); trie.PrintTrie();
Attempts:
2 left
💡 Hint
Think about how each word is stored in the Trie and when the end of a word is marked.
✗ Incorrect
The Trie stores all inserted words. After inserting "cat", "car", and "dog", all three words are present and printed because each insertion marks the end of the word.
❓ Predict Output
intermediateWhat is the output after inserting overlapping prefixes in a Trie?
Given the following insertions into an empty Trie: "bat", "bath", "baton", what words will be printed by the PrintTrie method?
DSA C#
public class TrieNode { public Dictionary<char, TrieNode> Children = new Dictionary<char, TrieNode>(); public bool IsEndOfWord = false; } public class Trie { private TrieNode root = new TrieNode(); public void Insert(string word) { TrieNode node = root; foreach (char c in word) { if (!node.Children.ContainsKey(c)) { node.Children[c] = new TrieNode(); } node = node.Children[c]; } node.IsEndOfWord = true; } public void PrintTrie() { PrintNode(root, ""); } private void PrintNode(TrieNode node, string prefix) { if (node.IsEndOfWord) { Console.WriteLine(prefix); } foreach (var child in node.Children) { PrintNode(child.Value, prefix + child.Key); } } } var trie = new Trie(); trie.Insert("bat"); trie.Insert("bath"); trie.Insert("baton"); trie.PrintTrie();
Attempts:
2 left
💡 Hint
Remember that each word insertion marks the end of the word even if prefixes overlap.
✗ Incorrect
All three words are inserted and marked as complete words in the Trie. The PrintTrie method prints all words where IsEndOfWord is true.
❓ Predict Output
advancedWhat is the output after inserting words with shared prefixes and printing in lex order?
After inserting "apple", "app", "application" into a Trie, what is the order of words printed by the PrintTrie method that prints words in lexicographical order?
DSA C#
public class TrieNode { public SortedDictionary<char, TrieNode> Children = new SortedDictionary<char, TrieNode>(); public bool IsEndOfWord = false; } public class Trie { private TrieNode root = new TrieNode(); public void Insert(string word) { TrieNode node = root; foreach (char c in word) { if (!node.Children.ContainsKey(c)) { node.Children[c] = new TrieNode(); } node = node.Children[c]; } node.IsEndOfWord = true; } public void PrintTrie() { PrintNode(root, ""); } private void PrintNode(TrieNode node, string prefix) { if (node.IsEndOfWord) { Console.WriteLine(prefix); } foreach (var child in node.Children) { PrintNode(child.Value, prefix + child.Key); } } } var trie = new Trie(); trie.Insert("apple"); trie.Insert("app"); trie.Insert("application"); trie.PrintTrie();
Attempts:
2 left
💡 Hint
The Trie uses a sorted dictionary for children, so words print in alphabetical order.
✗ Incorrect
The words print in lexicographical order: "app" (shortest prefix), then "apple", then "application".
🔧 Debug
advancedIdentify the error in this Trie insert method
What error will occur when running this insert method in a Trie implementation?
DSA C#
public void Insert(string word) {
TrieNode node = root;
foreach (char c in word) {
if (node.Children.ContainsKey(c)) {
node.Children[c] = new TrieNode();
}
node = node.Children[c];
}
node.IsEndOfWord = true;
}Attempts:
2 left
💡 Hint
Check the condition inside the if statement and what happens when it is true.
✗ Incorrect
The condition incorrectly creates a new node when the child already exists, overwriting it and losing previous data.
🧠 Conceptual
expertWhat is the time complexity of inserting a word of length n into a Trie?
Assuming a Trie is implemented with a hash map or dictionary for children nodes, what is the time complexity to insert a word of length n?
Attempts:
2 left
💡 Hint
Consider how many characters you process and how many dictionary lookups happen.
✗ Incorrect
Each character is processed once, and dictionary lookups are average O(1), so total time is O(n).
