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 by mistake.
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 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 'level' instead of 'index' to access children.
Using the character directly as an index.
✗ Incorrect
We check if the child at position 'index' is NULL before creating a new node.
3fill in blank
hardFix the error in the function that checks if a word 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[[1]])
return 0;
pCrawl = pCrawl->children[index];
}
return (pCrawl != NULL && pCrawl->isEndOfWord);
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking children at 'level' instead of 'index'.
Using the character directly as an index.
✗ Incorrect
We must check the child at 'index' to see if the path exists in the Trie.
4fill in blank
hardFill both blanks to implement the recursive function that finds the longest word in the Trie where all prefixes are valid words.
DSA C
void longestWordUtil(struct TrieNode* root, char* currentWord, int level, char* longestWord) {
if (root->isEndOfWord) {
currentWord[level] = '\0';
if (strlen(currentWord) > strlen(longestWord)) {
strcpy(longestWord, [1]);
}
}
for (int i = 0; i < 26; i++) {
if (root->children[i] && root->children[i]->isEndOfWord) {
currentWord[level] = 'a' + [2];
longestWordUtil(root->children[i], currentWord, level + 1, longestWord);
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Copying longestWord to currentWord instead of the other way.
Using 'level' instead of 'i' to calculate the character.
✗ Incorrect
We copy the currentWord to longestWord when a longer valid word is found, and use 'i' to get the character for the next level.
5fill in blank
hardFill all three blanks to complete the function that finds the longest word in the dictionary using the Trie.
DSA C
char* longestWord(char** words, int wordsSize) {
struct TrieNode* root = getNode();
for (int i = 0; i < wordsSize; i++) {
insert(root, [1]);
}
char currentWord[1000];
char* longestWord = (char*)malloc(1000 * sizeof(char));
longestWord[0] = '\0';
longestWordUtil(root, currentWord, 0, [2]);
return [3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Passing currentWord instead of longestWord to longestWordUtil.
Returning currentWord instead of longestWord.
Inserting wrong variable instead of words[i].
✗ Incorrect
Insert each word from words[i], pass longestWord to the utility function, and return longestWord at the end.
