Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a character into the Trie node's children array.
DSA C
node->children[[1]] = createNode();Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction for index calculation.
Using character directly as index without conversion.
✗ Incorrect
We subtract 'a' from the character to get the index 0-25 for lowercase letters.
2fill in blank
mediumComplete the code to check if a Trie node has a child for a given character.
DSA C
if (node->children[[1]] != NULL) { /* child exists */ }
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or multiplication instead of subtraction.
Checking wrong index leading to incorrect child detection.
✗ Incorrect
Subtracting 'a' from the character gives the correct index to check in children array.
3fill in blank
hardFix the error in the function that searches for a prefix in the Trie.
DSA C
for (int i = 0; i < length; i++) { if (node->children[[1]] == NULL) { return false; } node = node->children[[1]]; } return true;
Attempts:
3 left
💡 Hint
Common Mistakes
Using prefix[i] + 'a' which gives wrong index.
Using loop variable i as index directly.
✗ Incorrect
We must subtract 'a' from the character at prefix[i] to get the correct index.
4fill in blank
hardFill both blanks to create a Trie node and initialize its children to NULL.
DSA C
TrieNode* node = (TrieNode*)malloc(sizeof([1])); for (int i = 0; i < [2]; i++) { node->children[i] = NULL; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong struct name in malloc.
Initializing wrong number of children.
✗ Incorrect
We allocate memory for TrieNode and initialize 26 children for lowercase letters.
5fill in blank
hardFill all three blanks to implement a function that inserts a word into the Trie.
DSA C
void insert(TrieNode* root, const char* word) {
TrieNode* node = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = [1];
if (node->children[index] == NULL) {
node->children[index] = [2];
}
node = node->children[index];
}
node->isEndOfWord = [3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction for index.
Not creating new node when child is NULL.
Not marking end of word correctly.
✗ Incorrect
We calculate index by subtracting 'a', create a new node if needed, and mark end of word as true.
