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 the character directly without subtracting 'a'.
Using wrong character subtraction leading to wrong index.
✗ Incorrect
The character 'c' minus 'a' gives the correct index 2 for the children array.
2fill in blank
mediumComplete the code to check if a prefix exists in the Trie.
DSA C++
if (node->children[[1]] == nullptr) return false;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong index for the character.
Not checking for nullptr before accessing children.
✗ Incorrect
To check prefix starting with 'p', index is 'p' - 'a' = 15.
3fill in blank
hardFix the error in the code to mark the end of a word in the Trie.
DSA C++
node->[1] = true; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined or inconsistent variable names.
Forgetting to mark the end of word.
✗ Incorrect
The standard name used is 'isEnd' to mark the end of a word.
4fill in blank
hardFill both blanks to complete the loop that traverses the Trie for a prefix.
DSA C++
for (int i = 0; i < [1]; i++) { int index = prefix[i] [2] 'a'; if (node->children[index] == nullptr) return false; node = node->children[index]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' for index calculation.
Using wrong length function.
✗ Incorrect
Use prefix.length() for length and subtract 'a' to get index.
5fill in blank
hardFill all three blanks to complete the function that inserts a word into the Trie.
DSA C++
void insert(string word) {
TrieNode* node = root;
for (int i = 0; i < [1]; i++) {
int index = word[i] [2] 'a';
if (node->children[index] == nullptr) {
node->children[index] = new TrieNode();
}
node = node->children[index];
}
node->[3] = true;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' for index calculation.
Not marking the end of the word.
✗ Incorrect
Use word.size() for length, subtract 'a' for index, and mark isEnd true.