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: node[char] = [1] node = node[char] node['#'] = True
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list or set instead of a dictionary for child nodes.
Assigning None which does not allow further insertions.
✗ Incorrect
We use an empty dictionary {} to represent the next level nodes 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 char not in node: return False node = node[[1]] return True
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole prefix instead of the current character.
Using the wrong variable like 'node' or 'root' as key.
✗ Incorrect
We move to the child node corresponding to the current character 'char' in the prefix.
3fill in blank
hardFix the error in the code to count words with a given prefix using a hash map.
DSA Python
def count_prefix(words_map, prefix): count = 0 for word in words_map: if word.[1](prefix): count += words_map[word] return count
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which checks for exact match, not prefix.
Using 'in' which checks substring anywhere, not just prefix.
✗ Incorrect
Use the string method 'startswith' to check if 'word' begins with 'prefix'.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps words to their lengths for words longer than 3 characters.
DSA Python
{word: [1] for word in words if len(word) [2] 3}Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as value instead of its length.
Using '<' instead of '>' causing wrong filtering.
✗ Incorrect
We map each word to its length using len(word) and filter words longer than 3 using >.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps uppercase words to their counts for words with count > 1.
DSA Python
{ [1]: [2] for word, count in word_counts.items() if count [3] 1 }Attempts:
3 left
💡 Hint
Common Mistakes
Using word as key without uppercase conversion.
Using count <= 1 which filters out desired words.
✗ Incorrect
Keys are uppercase words using word.upper(), values are counts, filtered by count > 1.
