Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Trie after inserting words
What is the printed structure of the Trie after inserting the words "cat", "car", "dog" in order?
DSA Python
class TrieNode: def __init__(self): self.children = {} self.is_end = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_end = True def print_trie(self, node=None, word=''): if node is None: node = self.root if node.is_end: print(word) for char, child in node.children.items(): self.print_trie(child, word + char) trie = Trie() words = ["cat", "car", "dog"] for w in words: trie.insert(w) trie.print_trie()
Attempts:
2 left
💡 Hint
The print_trie method traverses children in dictionary insertion order (Python 3.7+).
✗ Incorrect
The Trie stores words by characters. The print_trie method recursively prints words when is_end is True. Children are iterated in insertion order (Python 3.7+). For 'cat' and 'car', the 't' child is inserted before the 'r' child under the 'ca' node, so 'cat' prints before 'car', followed by 'dog'. The correct output is:
cat
car
dog
❓ Predict Output
intermediateTrie node children count after insertions
After inserting the words "apple", "app", "ape" into an empty Trie, how many children does the root node have?
DSA Python
class TrieNode: def __init__(self): self.children = {} self.is_end = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_end = True trie = Trie() words = ["apple", "app", "ape"] for w in words: trie.insert(w) print(len(trie.root.children))
Attempts:
2 left
💡 Hint
All words start with the letter 'a'.
✗ Incorrect
All inserted words start with 'a', so the root node has only one child for 'a'.
🔧 Debug
advancedIdentify the error in Trie insert method
What error will this code raise when inserting the word "bat" into the Trie?
DSA Python
class TrieNode: def __init__(self): self.children = [] self.is_end = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_end = True trie = Trie() trie.insert("bat")
Attempts:
2 left
💡 Hint
Check the type of children and how it is accessed.
✗ Incorrect
children is a list, but code tries to access children[char] where char is a string, causing TypeError.
❓ Predict Output
advancedTrie structure after inserting overlapping words
After inserting "to", "tea", "ted", "ten" into an empty Trie, which words will be printed by this code?
DSA Python
class TrieNode: def __init__(self): self.children = {} self.is_end = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_end = True def print_words(self, node=None, prefix=''): if node is None: node = self.root if node.is_end: print(prefix) for char in sorted(node.children.keys()): self.print_words(node.children[char], prefix + char) trie = Trie() for w in ["to", "tea", "ted", "ten"]: trie.insert(w) trie.print_words()
Attempts:
2 left
💡 Hint
Children are printed in sorted order of keys at each node.
✗ Incorrect
The print_words method sorts children keys before recursion, so words print in lex order: 'tea', 'ted', 'ten', 'to'.
🧠 Conceptual
expertWhy use a Trie for prefix search?
Which of the following best explains why a Trie is efficient for prefix search compared to a list of strings?
Attempts:
2 left
💡 Hint
Think about how Trie nodes represent characters and how prefix search works.
✗ Incorrect
Trie allows prefix search by traversing nodes for each prefix character in O(k) time. Lists require checking each word, which is slower.
