Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Trie Insertion and Search
What is the output of the following code that inserts words into a Trie and searches for a prefix?
DSA C
typedef struct TrieNode {
struct TrieNode* children[26];
int isEndOfWord;
} TrieNode;
TrieNode* getNode() {
TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode));
node->isEndOfWord = 0;
for (int i = 0; i < 26; i++)
node->children[i] = NULL;
return node;
}
void insert(TrieNode* root, const char* key) {
TrieNode* pCrawl = root;
for (int i = 0; key[i] != '\0'; i++) {
int index = key[i] - 'a';
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
pCrawl->isEndOfWord = 1;
}
int searchPrefix(TrieNode* root, const char* prefix) {
TrieNode* pCrawl = root;
for (int i = 0; prefix[i] != '\0'; i++) {
int index = prefix[i] - 'a';
if (!pCrawl->children[index])
return 0;
pCrawl = pCrawl->children[index];
}
return 1;
}
int main() {
TrieNode* root = getNode();
insert(root, "apple");
insert(root, "app");
insert(root, "bat");
insert(root, "bath");
printf("%d\n", searchPrefix(root, "app"));
printf("%d\n", searchPrefix(root, "bat"));
printf("%d\n", searchPrefix(root, "bath"));
printf("%d\n", searchPrefix(root, "batman"));
return 0;
}Attempts:
2 left
💡 Hint
Check if the prefix exists in the Trie nodes after insertion.
✗ Incorrect
The prefixes "app", "bat", and "bath" exist in the Trie, so searchPrefix returns 1 for them. "batman" does not exist as a prefix, so it returns 0.
🧠 Conceptual
intermediateUnderstanding Trie Node Structure
Which statement correctly describes the memory usage of a Trie node in this implementation?
Attempts:
2 left
💡 Hint
Look at the struct definition and what fields it contains.
✗ Incorrect
The TrieNode struct has an array of 26 pointers for each letter and an integer flag isEndOfWord.
🔧 Debug
advancedIdentify the Bug in Trie Search Function
What error will occur when running this search function if the prefix contains uppercase letters?
DSA C
int searchPrefix(TrieNode* root, const char* prefix) { TrieNode* pCrawl = root; for (int i = 0; prefix[i] != '\0'; i++) { int index = prefix[i] - 'a'; if (!pCrawl->children[index]) return 0; pCrawl = pCrawl->children[index]; } return 1; }
Attempts:
2 left
💡 Hint
Consider ASCII values of uppercase letters and how index is calculated.
✗ Incorrect
Uppercase letters have ASCII codes less than 'a', so index becomes negative, causing invalid array access.
🚀 Application
advancedAutocomplete Suggestions Using Trie
Given a Trie with words inserted, which approach correctly collects all words starting with a given prefix?
Attempts:
2 left
💡 Hint
Think about efficient traversal starting from the prefix node.
✗ Incorrect
The efficient way is to find the node matching the prefix, then recursively collect all words below it.
❓ Predict Output
expertOutput of Trie Deletion Operation
What is the output after deleting the word "app" from the Trie containing "apple", "app", "application" and then searching for "app" and "apple"?
DSA C
int deleteWord(TrieNode* root, const char* key, int depth) { if (!root) return 0; if (key[depth] == '\0') { if (root->isEndOfWord) root->isEndOfWord = 0; for (int i = 0; i < 26; i++) if (root->children[i]) return 0; return 1; } int index = key[depth] - 'a'; if (deleteWord(root->children[index], key, depth + 1)) { free(root->children[index]); root->children[index] = NULL; if (!root->isEndOfWord) { for (int i = 0; i < 26; i++) if (root->children[i]) return 0; return 1; } else return 0; } return 0; } int main() { TrieNode* root = getNode(); insert(root, "apple"); insert(root, "app"); insert(root, "application"); deleteWord(root, "app", 0); printf("%d\n", searchPrefix(root, "app")); printf("%d\n", searchPrefix(root, "apple")); return 0; }
Attempts:
2 left
💡 Hint
Deleting "app" should not remove "apple" or "application" prefixes.
✗ Incorrect
After deleting "app", the prefix "app" still exists because "apple" and "application" share it. So searchPrefix("app") returns 1 and searchPrefix("apple") returns 1.
