Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Searching Words in Trie
What is the output of the following code that inserts words into a Trie and searches for some words?
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 search(self, word): node = self.root for char in word: if char not in node.children: return False node = node.children[char] return node.is_end trie = Trie() words_to_insert = ["cat", "car", "cart", "dog"] for w in words_to_insert: trie.insert(w) search_words = ["car", "cart", "cap", "dog", "do"] results = {w: trie.search(w) for w in search_words} print(results)
Attempts:
2 left
💡 Hint
Remember that search returns True only if the whole word is inserted and marked as end.
✗ Incorrect
The words 'car', 'cart', and 'dog' were inserted fully, so search returns True for them. 'cap' and 'do' were not inserted as complete words, so search returns False.
🧠 Conceptual
intermediateUnderstanding Trie Node Structure
Which of the following best describes the role of the 'is_end' flag in a Trie node?
Attempts:
2 left
💡 Hint
Think about how the Trie knows when a word finishes.
✗ Incorrect
The 'is_end' flag is True only at nodes where a complete word ends, distinguishing prefixes from full words.
🔧 Debug
advancedIdentify the Error in Trie Search Method
What error will occur when running this search method in a Trie if the word is not found?
DSA Python
def search(self, word): node = self.root for char in word: node = node.children[char] return node.is_end
Attempts:
2 left
💡 Hint
What happens if a character is missing in the children dictionary?
✗ Incorrect
Accessing node.children[char] without checking if char exists causes KeyError if char is missing.
🚀 Application
advancedNumber of Words with Given Prefix in Trie
Given a Trie with words inserted, which method correctly counts how many words start with a given prefix?
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 count_prefix(self, prefix): node = self.root for char in prefix: if char not in node.children: return 0 node = node.children[char] return self._count_words(node) def _count_words(self, node): count = 1 if node.is_end else 0 for child in node.children.values(): count += self._count_words(child) return count
Attempts:
2 left
💡 Hint
The method counts words from the node reached after prefix traversal.
✗ Incorrect
The count_prefix method finds the node for the prefix, then recursively counts all words below it, giving the number of words starting with that prefix.
❓ Predict Output
expertOutput of Trie After Insertions and Searches
What is the output of this code that inserts words and searches for prefixes and words in a 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 def search(self, word): node = self.root for char in word: if char not in node.children: return False node = node.children[char] return node.is_end def starts_with(self, prefix): node = self.root for char in prefix: if char not in node.children: return False node = node.children[char] return True trie = Trie() words = ["apple", "app", "application", "bat", "batch"] for w in words: trie.insert(w) results = { "search_app": trie.search("app"), "search_appl": trie.search("appl"), "starts_with_app": trie.starts_with("app"), "starts_with_bat": trie.starts_with("bat"), "search_batch": trie.search("batch") } print(results)
Attempts:
2 left
💡 Hint
Search returns True only for complete words; starts_with returns True if prefix exists.
✗ Incorrect
The word 'app' is inserted fully, so search("app") is True. 'appl' is a prefix but not a full word, so search("appl") is False. starts_with("app") and starts_with("bat") are True because those prefixes exist. 'batch' is inserted fully, so search("batch") is True.
