Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a pointer to a Trie node.
DSA C
struct TrieNode* [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names that do not represent the root node.
Forgetting to declare a pointer type.
✗ Incorrect
The pointer to the root node of the Trie is commonly named 'root'.
2fill in blank
mediumComplete the code to allocate memory for a new Trie node.
DSA C
struct TrieNode* node = (struct TrieNode*) malloc(sizeof([1]));Attempts:
3 left
💡 Hint
Common Mistakes
Using 'struct' keyword inside sizeof incorrectly.
Using incorrect struct names.
✗ Incorrect
We allocate memory for the size of the TrieNode structure.
3fill in blank
hardFix the error in the code to initialize all children pointers of a Trie node to NULL.
DSA C
for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[[1]] = NULL; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a constant or wrong variable as index.
Using the pointer name instead of index.
✗ Incorrect
The loop variable 'i' is used to index the children array.
4fill in blank
hardFill both blanks to complete the function that creates and initializes a new Trie node.
DSA C
struct TrieNode* getNode() {
struct TrieNode* node = (struct TrieNode*) malloc(sizeof([1]));
node->isEndOfWord = [2];
for (int i = 0; i < ALPHABET_SIZE; i++) {
node->children[i] = NULL;
}
return node;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong struct name in sizeof.
Setting isEndOfWord to 1 by mistake.
✗ Incorrect
We allocate memory for TrieNode and initialize isEndOfWord to 0 (false).
5fill in blank
hardFill all three blanks to complete the function that inserts a key into the Trie.
DSA C
void insert(struct TrieNode* root, const char* key) {
struct TrieNode* node = root;
for (int i = 0; key[i] != '\0'; i++) {
int index = key[i] - 'a';
if (node->children[[1]] == NULL) {
node->children[[2]] = getNode();
}
node = node->children[[3]];
}
node->isEndOfWord = 1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using loop variable 'i' instead of 'index' for children array.
Using constant 0 or root pointer incorrectly.
✗ Incorrect
We use 'index' to access the correct child for each character.
