Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a character into the Trie node's children.
DSA C++
node->children[[1]] = new TrieNode(); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using character directly without subtracting 'a'.
Using wrong character subtraction causing out of range index.
✗ Incorrect
The character 'b' minus 'a' gives the correct index 1 for the children array.
2fill in blank
mediumComplete the code to check if the current Trie node marks the end of a word.
DSA C++
if (node->[1]) { return true; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect or undefined flag names.
Checking for null pointer instead of boolean flag.
✗ Incorrect
The standard boolean flag used is 'isEnd' to mark the end of a word in the Trie node.
3fill in blank
hardFix the error in the search function to correctly traverse the Trie for each character.
DSA C++
node = node->children[[1]]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the character directly as index causing out of bounds.
Using the loop index 'i' as index instead of character mapping.
✗ Incorrect
We must convert the character to an index by subtracting 'a' to access the correct child node.
4fill in blank
hardFill both blanks to complete the recursive helper function for word search in Trie.
DSA C++
bool searchHelper(TrieNode* node, const string& word, int [1]) { if ([2] == word.size()) return node->isEnd; int index = word[[1]] - 'a'; if (!node->children[index]) return false; return searchHelper(node->children[index], word, [1] + 1); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing mismatch.
Comparing wrong variable to word size.
✗ Incorrect
The variable 'pos' is used to track the current position in the word for recursion.
5fill in blank
hardFill all three blanks to complete the insert function for adding a word to the Trie.
DSA C++
void insert(string word) {
TrieNode* node = root;
for (int [1] = 0; [2] < word.size(); [3]++) {
int index = word[[2]] - 'a';
if (!node->children[index]) {
node->children[index] = new TrieNode();
}
node = node->children[index];
}
node->isEnd = true;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in loop causing errors.
Incorrect loop increment syntax.
✗ Incorrect
The loop variable 'i' is used consistently for initialization, condition, and increment.