Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a word into the trie.
DSA Python
def insert_word(root, word): node = root for char in word: if char not in node.children: node.children[[1]] = TrieNode() node = node.children[char]
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole word instead of the current character.
Using the root or node variable incorrectly as a key.
✗ Incorrect
We use the current character 'char' as the key to add a new child node in the trie.
2fill in blank
mediumComplete the code to check if a prefix exists in the trie.
DSA Python
def starts_with(root, prefix): node = root for char in prefix: if [1] not in node.children: return False node = node.children[char] return True
Attempts:
3 left
💡 Hint
Common Mistakes
Checking the whole prefix instead of the current character.
Using the wrong variable to check children.
✗ Incorrect
We check if the current character 'char' is in the children of the current node.
3fill in blank
hardFix the error in the code to mark the end of a word in the trie.
DSA Python
def insert_word(root, word): node = root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.[1] = True
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect attribute names that do not exist in the TrieNode.
Forgetting to mark the end of the word.
✗ Incorrect
The standard attribute to mark the end of a word is 'is_end_of_word'.
4fill in blank
hardFill both blanks to complete the search function that returns True if a word exists in the trie.
DSA Python
def search_word(root, word): node = root for char in word: if [1] not in node.children: return False node = node.children[[2]] return node.is_end_of_word
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for checking and accessing children.
Using the whole word or wrong variables instead of the current character.
✗ Incorrect
We check and access the child node using the current character 'char' in the word.
5fill in blank
hardFill both blanks to create a dictionary comprehension that maps each word to True if it starts with the prefix.
DSA Python
result = {word{BLANK_1}}: True for word in words if word{{BLANK_2}} prefix:Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in' instead of equality for prefix check.
Incorrect slicing syntax.
✗ Incorrect
We slice the word up to the prefix length and check if it equals the prefix, then map word to True.
