Challenge - 5 Problems
Prefix Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Trie Insert and Search
What is the output of the following code that inserts words into a Trie and searches for a 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 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"] for w in words: trie.insert(w) print(trie.starts_with("app")) print(trie.starts_with("bat")) print(trie.starts_with("cat"))
Attempts:
2 left
💡 Hint
Check if each prefix exists by traversing the Trie nodes.
✗ Incorrect
The method starts_with returns True if the prefix exists in the Trie. "app" and "bat" prefixes exist, but "cat" does not.
🧠 Conceptual
intermediateNumber of Words with Given Prefix
In a Trie storing the words ["dog", "door", "dorm", "cat"], how many words start with the prefix "do"?
Attempts:
2 left
💡 Hint
Count words that begin with 'do' in the list.
✗ Incorrect
Words starting with "do" are "dog", "door", and "dorm" so total 3 words.
🔧 Debug
advancedFind the Bug in Trie Search Method
What error will the following code raise when searching for prefix "car" after inserting words ["cat", "cap"]?
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 starts_with(self, prefix): node = self.root for char in prefix: node = node.children[char] return True trie = Trie() for w in ["cat", "cap"]: trie.insert(w) print(trie.starts_with("car"))
Attempts:
2 left
💡 Hint
Check what happens if a character is missing in children dictionary.
✗ Incorrect
The code tries to access node.children[char] without checking if char exists. For prefix "car", 'r' is missing, causing a KeyError.
🚀 Application
advancedFind All Words with Prefix Using Trie
Given a Trie with words ["tree", "trie", "algo", "assoc", "all"], which option correctly returns all words starting with prefix "tr"?
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 _dfs(self, node, prefix, words): if node.is_end: words.append(prefix) for char, child in node.children.items(): self._dfs(child, prefix + char, words) def find_words_with_prefix(self, prefix): node = self.root for char in prefix: if char not in node.children: return [] node = node.children[char] words = [] self._dfs(node, prefix, words) return words trie = Trie() for w in ["tree", "trie", "algo", "assoc", "all"]: trie.insert(w) print(trie.find_words_with_prefix("tr"))
Attempts:
2 left
💡 Hint
Use DFS from the node matching the prefix to collect words.
✗ Incorrect
The method finds the node for prefix "tr" and collects all words below it: "tree" and "trie".
🧠 Conceptual
expertTrie Memory Usage Comparison
Which statement about memory usage of a Trie compared to a list of strings is correct when storing many words with common prefixes?
Attempts:
2 left
💡 Hint
Think about how common prefixes are stored once in a Trie.
✗ Incorrect
Trie nodes share common prefixes, reducing memory compared to storing full words separately in a list.
