Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new Trie node.
DSA C
struct TrieNode* getNode() {
struct TrieNode* pNode = (struct TrieNode*)malloc(sizeof(struct TrieNode));
pNode->isEndOfWord = [1];
for (int i = 0; i < 26; i++)
pNode->children[i] = NULL;
return pNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting isEndOfWord to 1 initially, which incorrectly marks the node as end of a word.
✗ Incorrect
When creating a new Trie node, isEndOfWord should be initialized to 0 (false) because no word ends at this node yet.
2fill in blank
mediumComplete the code to move to the correct child node for the current character.
DSA C
void insert(struct TrieNode* root, const char* key) {
struct TrieNode* pCrawl = root;
for (int level = 0; key[level] != '\0'; level++) {
int index = key[level] - 'a';
if (!pCrawl->children[[1]])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
pCrawl->isEndOfWord = 1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the loop variable 'level' directly as index.
Using the character 'key[level]' instead of its index.
✗ Incorrect
We use 'index' to access the correct child node corresponding to the current character.
3fill in blank
hardFix the error in the insert function to correctly mark the end of the word.
DSA C
void insert(struct TrieNode* root, const char* key) {
struct TrieNode* pCrawl = root;
for (int level = 0; key[level] != '\0'; level++) {
int index = key[level] - 'a';
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
pCrawl->isEndOfWord = [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting isEndOfWord to 0, which means the node is not end of a word.
✗ Incorrect
To mark the end of a word in the Trie, isEndOfWord should be set to 1 (true).
4fill in blank
hardFill both blanks to complete the insert function that adds a word to the Trie.
DSA C
void insert(struct TrieNode* root, const char* key) {
struct TrieNode* pCrawl = root;
for (int [1] = 0; key[[2]] != '\0'; [1]++) {
int index = key[[1]] - 'a';
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
pCrawl->isEndOfWord = 1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop declaration and condition.
Using an undeclared variable in the condition.
✗ Incorrect
The loop variable 'level' is used consistently to index the characters in the key string.
5fill in blank
hardFill all three blanks to complete the insert function with correct indexing and marking end of word.
DSA C
void insert(struct TrieNode* root, const char* key) {
struct TrieNode* pCrawl = root;
for (int [1] = 0; key[[2]] != '\0'; [1]++) {
int index = key[[3]] - 'a';
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
pCrawl->isEndOfWord = 1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Mixing different variable names for indexing inside the loop.
✗ Incorrect
The same variable 'level' is used for loop control and indexing the key string consistently.
