Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Prefix Search Result
What is the output of the prefix search for "app" after inserting the words "apple", "app", "application", and "apt" into the trie?
DSA C
/* Insert words into trie: apple, app, application, apt */ /* Then search for prefix "app" and list all words with this prefix */ Output format: JSON array of strings sorted lexicographically.
Attempts:
2 left
💡 Hint
Remember that only words starting exactly with the prefix "app" should be included.
✗ Incorrect
The prefix "app" matches words starting with "app". "apt" starts with "ap" but not "app", so it is excluded.
🧠 Conceptual
intermediateNumber of Nodes in Trie After Insertions
After inserting the words "cat", "car", "cart", and "dog" into an empty trie, how many nodes does the trie contain?
Attempts:
2 left
💡 Hint
Count shared prefixes carefully to avoid double counting nodes.
✗ Incorrect
The trie nodes are: root + c + a + t + r + d + o + g. The path for "car" and "cart" shares nodes c->a->r, so total nodes are 9.
🔧 Debug
advancedIdentify the Bug in Trie Insertion
What error will occur when running this trie insertion code snippet?
```c
void insert(struct TrieNode* root, const char* word) {
struct TrieNode* node = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = word[i] - 'a';
if (!node->children[index])
node->children[index] = malloc(sizeof(struct TrieNode));
node = node->children[index];
}
node->isEndOfWord = 1;
}
```
Assume struct TrieNode has a children array of 26 pointers and an int isEndOfWord.
DSA C
void insert(struct TrieNode* root, const char* word) {
struct TrieNode* node = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = word[i] - 'a';
if (!node->children[index])
node->children[index] = malloc(sizeof(struct TrieNode));
node = node->children[index];
}
node->isEndOfWord = 1;
}Attempts:
2 left
💡 Hint
Check what happens to the children pointers of newly allocated nodes.
✗ Incorrect
New nodes are allocated but their children pointers are not initialized to NULL, so accessing them later causes segmentation fault.
📝 Syntax
advancedCorrect Syntax for Trie Node Initialization
Which option correctly initializes a new trie node in C with all children pointers set to NULL and isEndOfWord set to 0?
Attempts:
2 left
💡 Hint
calloc initializes allocated memory to zero.
✗ Incorrect
Option B uses calloc which sets all bytes to zero, so children pointers become NULL and isEndOfWord is 0.
🚀 Application
expertLongest Common Prefix Using Trie
Given the words ["flower", "flow", "flight"], what is the longest common prefix found using a trie?
Attempts:
2 left
💡 Hint
The longest common prefix is the longest path from root where all words share the same character.
✗ Incorrect
All words start with 'fl', but 'flow' and 'flower' share 'flo', 'flight' does not, so longest common prefix is "fl".
