Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new Trie node.
DSA C
TrieNode* createNode() {
TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode));
node->isEndOfWord = [1];
for (int i = 0; i < 26; i++) {
node->children[i] = NULL;
}
return node;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting isEndOfWord to 1 by mistake, which means the node is end of a word.
Using NULL instead of 0 for a boolean flag.
✗ Incorrect
The isEndOfWord flag should be initialized to 0 (false) when creating a new Trie node.
2fill in blank
mediumComplete the code to insert a character into the Trie children array.
DSA C
void insertChar(TrieNode* node, char c) {
int index = c - 'a';
if (node->children[[1]] == NULL) {
node->children[index] = createNode();
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the character c directly as an index.
Using 0 instead of the calculated index.
✗ Incorrect
The index variable holds the position for the character c in the children array.
3fill in blank
hardFix the error in the search function to check if a character exists in the Trie.
DSA C
int search(TrieNode* root, const char* word) {
TrieNode* current = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = word[i] - 'a';
if (current->children[[1]] == NULL) {
return 0;
}
current = current->children[index];
}
return current->isEndOfWord;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the character word[i] directly as an index.
Using the loop variable i as index.
✗ Incorrect
We must use the calculated index to check the children array for the character.
4fill in blank
hardFill both blanks to implement the recursive function to collect all words from a given Trie node.
DSA C
void collectWords(TrieNode* node, char* prefix, int length, char results[][100], int* count) { if (node->isEndOfWord) { prefix[length] = '\0'; strcpy(results[*count], [1]); (*count)++; } for (int i = 0; i < 26; i++) { if (node->children[i]) { prefix[length] = [2]; collectWords(node->children[i], prefix, length + 1, results, count); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Copying results instead of prefix.
Using i directly as a character instead of 'a' + i.
✗ Incorrect
Copy the current prefix string to results, and add the character 'a' + i to prefix before recursion.
5fill in blank
hardFill all three blanks to implement the autocomplete function that returns all words starting with a prefix.
DSA C
void autocomplete(TrieNode* root, const char* prefix, char results[][100], int* count) { TrieNode* current = root; for (int i = 0; prefix[i] != '\0'; i++) { int index = prefix[i] - 'a'; if (current->children[[1]] == NULL) { return; } current = current->children[index]; } char buffer[100]; strcpy(buffer, [2]); collectWords(current, buffer, [3], results, count); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using prefix[i] instead of index to check children.
Passing 0 instead of prefix length to collectWords.
Not copying prefix to buffer before collecting words.
✗ Incorrect
Use index to check children, copy prefix to buffer, and pass prefix length to collectWords.
