Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a character into the children map of the current Trie node.
DSA C++
current->children['a'] = new TrieNode(); current = current->children[[1]];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different character than the one inserted.
Forgetting to move current to the new node.
✗ Incorrect
We insert the character 'a' into the children map and then move current to that child node.
2fill in blank
mediumComplete the code to check if the current character exists in the children map.
DSA C++
if (current->children.find(word[[1]]) == current->children.end()) { current->children[word[i]] = new TrieNode(); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed index like 0 or 1 instead of the loop variable.
Using the whole word instead of a single character.
✗ Incorrect
We check if the character at index i in the word exists in the children map.
3fill in blank
hardFix the error in marking the end of a word in the Trie node.
DSA C++
current->[1] = true; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not exist in the TrieNode class.
Forgetting to mark the end of the word.
✗ Incorrect
The correct member variable to mark the end of a word is 'isEnd'.
4fill in blank
hardFill both blanks to complete the loop that inserts each character of the word into the Trie.
DSA C++
for (int [1] = 0; [2] < word.size(); [2]++) { if (current->children.find(word[i]) == current->children.end()) { current->children[word[i]] = new TrieNode(); } current = current->children[word[i]]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop declaration and condition.
Using a variable name not used inside the loop body.
✗ Incorrect
The loop variable should be 'i' consistently for initialization, condition, and increment.
5fill in blank
hardFill all three blanks to complete the insert function for the Trie.
DSA C++
void insert(string [1]) { TrieNode* current = root; for (int i = 0; i < [2].size(); i++) { if (current->children.find([3][i]) == current->children.end()) { current->children[[3][i]] = new TrieNode(); } current = current->children[[3][i]]; } current->isEnd = true; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inside the function than the parameter.
Forgetting to mark the end of the word.
✗ Incorrect
The parameter name and all references inside the function should be consistent, here 'str'.