Challenge - 5 Problems
Autocomplete Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Trie Insertion 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 = ["dog", "deer", "deal"] for w in words: trie.insert(w) print(trie.starts_with("de")) print(trie.starts_with("da"))
Attempts:
2 left
💡 Hint
Check if the prefix exists in the Trie nodes after insertion.
✗ Incorrect
The words 'deer' and 'deal' start with 'de', so starts_with('de') returns True. No word starts with 'da', so starts_with('da') returns False.
🧠 Conceptual
intermediateUnderstanding Trie Node Structure
Which of the following best describes the structure of a Trie node used in an autocomplete system?
Attempts:
2 left
💡 Hint
Think about how to efficiently check prefixes and word endings.
✗ Incorrect
Each Trie node holds children nodes in a dictionary keyed by characters and a boolean to mark if a word ends there, enabling prefix and word searches.
🔧 Debug
advancedIdentify the Bug in Autocomplete Suggestion Code
What error will the following code produce when trying to get autocomplete suggestions for prefix 'ca'?
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, results): if node.is_end: results.append(prefix) for char in node.children: self._dfs(node.children[char], prefix + char, results) def autocomplete(self, prefix): node = self.root for char in prefix: if char not in node.children: return [] node = node.children[char] results = [] self._dfs(node, prefix, results) return results trie = Trie() words = ["cat", "car", "cart", "dog"] for w in words: trie.insert(w) print(trie.autocomplete("ca"))
Attempts:
2 left
💡 Hint
Check if the DFS method correctly traverses children nodes.
✗ Incorrect
The code correctly inserts words and performs DFS to collect all words starting with 'ca'. No error occurs and output is ['cat', 'car', 'cart'].
🚀 Application
advancedNumber of Words with Given Prefix
Given a Trie with inserted words ['apple', 'app', 'application', 'bat', 'ball'], what is the number of words that start with prefix 'app'?
Attempts:
2 left
💡 Hint
Count all words that start with 'app' including the prefix itself if it is a word.
✗ Incorrect
'apple', 'app', and 'application' all start with 'app', so the count is 3.
🧠 Conceptual
expertWhy Use Trie for Autocomplete Instead of Hash Map?
Why is a Trie data structure preferred over a hash map for implementing an autocomplete system?
Attempts:
2 left
💡 Hint
Consider how prefix queries are handled in both data structures.
✗ Incorrect
Tries are designed to share common prefixes and allow fast prefix-based searches, which hash maps cannot do efficiently without scanning all keys.
