Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the root node of the Trie.
DSA C++
struct TrieNode {
TrieNode* children[26];
bool isEndOfWord;
TrieNode() {
for (int i = 0; i < 26; i++) {
children[i] = nullptr;
}
isEndOfWord = false;
}
};
TrieNode* [1] = new TrieNode(); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names that do not clearly represent the root node.
Not initializing the root node properly.
✗ Incorrect
The root of the Trie is commonly named 'root' to represent the starting point of the Trie structure.
2fill in blank
mediumComplete the code to insert a character into the Trie children array.
DSA C++
void insertWord(TrieNode* root, const string& word) {
TrieNode* current = root;
for (char c : word) {
int index = c - 'a';
if (current->children[[1]] == nullptr) {
current->children[index] = new TrieNode();
}
current = current->children[index];
}
current->isEndOfWord = true;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the character 'c' directly as an index.
Using a fixed index like 0 instead of the calculated index.
✗ Incorrect
The index variable holds the position in the children array corresponding to the character c.
3fill in blank
hardFix the error in the search function to correctly check if a word exists in the Trie.
DSA C++
bool searchWord(TrieNode* root, const string& word) {
TrieNode* current = root;
for (char c : word) {
int index = c - 'a';
if (current->children[[1]] == nullptr) {
return false;
}
current = current->children[index];
}
return current->isEndOfWord;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking children[c] instead of children[index].
Using a fixed index like 0.
✗ Incorrect
The search must check the children array at the position given by index, which corresponds to the character c.
4fill in blank
hardFill both blanks to complete the recursive function that collects all words starting from a given Trie node.
DSA C++
void collectWords(TrieNode* node, string prefix, vector<string>& results) {
if (node->[1]) {
results.push_back(prefix);
}
for (int i = 0; i < 26; i++) {
if (node->children[i] != nullptr) {
collectWords(node->children[i], prefix + [2], results);
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong member name for the end of word flag.
Appending an integer instead of a character to the prefix.
✗ Incorrect
The function checks if the current node marks the end of a word using isEndOfWord, and appends the character corresponding to index i by converting 'a' + i to a char.
5fill in blank
hardFill both blanks to complete the autocomplete function that returns all words with a given prefix.
DSA C++
vector<string> autocomplete(TrieNode* root, const string& prefix) {
TrieNode* current = root;
for (char c : prefix) {
int index = c - 'a';
if (current->children[[1]] == nullptr) {
return {};
}
current = current->children[index];
}
vector<string> results;
collectWords(current, [2], results);
return results;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index to access children.
Passing an empty string instead of the prefix to collectWords.
Returning the wrong variable.
✗ Incorrect
The function uses the index to traverse children, passes the prefix string to collectWords to start collecting words, and returns the results vector.