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] node.is_end = True
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole word as a key instead of the current character.
Using the root or node variable incorrectly.
✗ Incorrect
We use the current character 'char' as the key to insert into the children dictionary.
2fill in blank
mediumComplete the code to check if a node marks the end of a word.
DSA Python
def is_word_end(node): return node.[1]
Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'children' instead of 'is_end'.
Using undefined attributes.
✗ Incorrect
The 'is_end' attribute tells if the node marks the end of a word.
3fill in blank
hardFix the error in the code that finds the longest word in the Trie.
DSA Python
def longest_word(root): result = "" def dfs(node, path): nonlocal result if not node.is_end and node != root: return if len(path) > len(result) or (len(path) == len(result) and path < result): result = path for char in sorted(node.children.keys()): dfs(node.children[[1]], path + char) dfs(root, "") return result
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'path' or 'node' instead of 'char' as key.
Accessing children with wrong variable.
✗ Incorrect
We must use 'char' to access the child node in the children dictionary.
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
words = ["apple", "bat", "cat", "applesauce"] lengths = { [1]: [2] for word in words if len(word) > 3 }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len' as key instead of word.
Using 'word.upper()' which changes the key.
✗ Incorrect
The dictionary keys are words and values are their lengths.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths for words longer than 3 characters.
DSA Python
words = ["apple", "bat", "cat", "applesauce"] result = { [1]: [2] for word in words if len(word) [3] 3 }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Using 'word' instead of 'word.upper()' as key.
✗ Incorrect
Keys are uppercase words, values are lengths, and condition checks length greater than 3.
