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 insert a word into the Trie.
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 instead of index to access children.
✗ Incorrect
We check if the child at position 'index' is NULL before creating a new node.
3fill in blank
hardFix the error in the search function to correctly check if a prefix exists in the Trie.
DSA C
int search(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])
return 0;
pCrawl = pCrawl->children[index];
}
return [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning pCrawl->isEndOfWord which only checks full words, not prefixes.
✗ Incorrect
For prefix search, we return 1 if all characters are found, regardless of isEndOfWord.
4fill in blank
hardFill both blanks to implement a function that counts how many words start with a given prefix.
DSA C
int countWordsWithPrefix(struct TrieNode* root, const char* prefix) {
struct TrieNode* pCrawl = root;
for (int level = 0; prefix[level] != '\0'; level++) {
int index = prefix[level] - 'a';
if (!pCrawl->children[index])
return 0;
pCrawl = pCrawl->children[index];
}
return [1](pCrawl);
}
int countWords(struct TrieNode* node) {
int count = 0;
if (node->isEndOfWord)
count++;
for (int i = 0; i < 26; i++) {
if (node->children[i])
count += [2](node->children[i]);
}
return count;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using insert or search functions instead of counting recursively.
✗ Incorrect
The function recursively counts words starting from the prefix node using countWords.
5fill in blank
hardFill all three blanks to implement a function that deletes a word from the Trie and cleans up unused nodes.
DSA C
int deleteWord(struct TrieNode* node, const char* key, int depth) {
if (!node)
return 0;
if (key[depth] == '\0') {
if (node->isEndOfWord)
node->isEndOfWord = 0;
if ([1](node)) {
free(node);
return 1;
}
return 0;
}
int index = key[depth] - 'a';
if (deleteWord(node->children[[2]], key, depth + 1)) {
node->children[index] = NULL;
if (!node->isEndOfWord && [3](node)) {
free(node);
return 1;
}
}
return 0;
}
int isEmpty(struct TrieNode* node) {
for (int i = 0; i < 26; i++)
if (node->children[i])
return 0;
return 1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable for child access.
Not checking if node is empty before freeing.
✗ Incorrect
We use isEmpty to check if node has no children, index to access child, and isEmpty again to check if parent can be freed.
