Challenge - 5 Problems
Prefix Count Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of prefix count after insertions
What is the output of the code after inserting words and counting the prefix 'app'?
DSA Python
class TrieNode: def __init__(self): self.children = {} self.count = 0 class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for ch in word: if ch not in node.children: node.children[ch] = TrieNode() node = node.children[ch] node.count += 1 def count_prefix(self, prefix): node = self.root for ch in prefix: if ch not in node.children: return 0 node = node.children[ch] return node.count trie = Trie() words = ['apple', 'app', 'application', 'apt', 'banana'] for w in words: trie.insert(w) print(trie.count_prefix('app'))
Attempts:
2 left
💡 Hint
Count how many words start with 'app' in the list.
✗ Incorrect
The words starting with 'app' are 'apple', 'app', and 'application', so the count is 3.
❓ Predict Output
intermediateCount prefix with no matching words
What is the output of counting prefix 'bat' after inserting given words?
DSA Python
class TrieNode: def __init__(self): self.children = {} self.count = 0 class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for ch in word: if ch not in node.children: node.children[ch] = TrieNode() node = node.children[ch] node.count += 1 def count_prefix(self, prefix): node = self.root for ch in prefix: if ch not in node.children: return 0 node = node.children[ch] return node.count trie = Trie() words = ['cat', 'car', 'cart', 'dog'] for w in words: trie.insert(w) print(trie.count_prefix('bat'))
Attempts:
2 left
💡 Hint
Check if any word starts with 'bat'.
✗ Incorrect
No word in the list starts with 'bat', so the count is 0.
❓ Predict Output
advancedCount prefix after multiple insertions with overlapping prefixes
What is the output after inserting words and counting prefix 'a'?
DSA Python
class TrieNode: def __init__(self): self.children = {} self.count = 0 class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for ch in word: if ch not in node.children: node.children[ch] = TrieNode() node = node.children[ch] node.count += 1 def count_prefix(self, prefix): node = self.root for ch in prefix: if ch not in node.children: return 0 node = node.children[ch] return node.count trie = Trie() words = ['a', 'ab', 'abc', 'abcd', 'abcde'] for w in words: trie.insert(w) print(trie.count_prefix('a'))
Attempts:
2 left
💡 Hint
Count all words starting with 'a'.
✗ Incorrect
All 5 words start with 'a', so the count is 5.
❓ Predict Output
advancedCount prefix after inserting words with shared and unique prefixes
What is the output of counting prefix 'ban' after inserting the words?
DSA Python
class TrieNode: def __init__(self): self.children = {} self.count = 0 class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for ch in word: if ch not in node.children: node.children[ch] = TrieNode() node = node.children[ch] node.count += 1 def count_prefix(self, prefix): node = self.root for ch in prefix: if ch not in node.children: return 0 node = node.children[ch] return node.count trie = Trie() words = ['banana', 'band', 'bandana', 'ban', 'banner', 'bat'] for w in words: trie.insert(w) print(trie.count_prefix('ban'))
Attempts:
2 left
💡 Hint
Count words starting with 'ban' only.
✗ Incorrect
The words starting with 'ban' are 'banana', 'band', 'bandana', 'ban', and 'banner' totaling 5.
🧠 Conceptual
expertUnderstanding Trie node count increments
In a Trie implementation for counting words with a given prefix, why is the count incremented at each node during insertion?
Attempts:
2 left
💡 Hint
Think about how prefix counts are retrieved efficiently.
✗ Incorrect
Incrementing count at each node during insertion tracks how many words share that prefix, allowing quick retrieval of prefix counts.
