Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after inserting words into a Trie
What is the printed output of the Trie structure after inserting the words
"cat", "car", and "dog" in this C++ code?DSA C++
struct TrieNode {
TrieNode* children[26] = {nullptr};
bool isEnd = false;
};
class Trie {
public:
TrieNode* root;
Trie() { root = new TrieNode(); }
void insert(const std::string& word) {
TrieNode* node = root;
for (char c : word) {
int index = c - 'a';
if (!node->children[index])
node->children[index] = new TrieNode();
node = node->children[index];
}
node->isEnd = true;
}
void printTrie(TrieNode* node, std::string prefix) {
if (node->isEnd) {
std::cout << prefix << "\n";
}
for (int i = 0; i < 26; i++) {
if (node->children[i]) {
printTrie(node->children[i], prefix + char('a' + i));
}
}
}
};
int main() {
Trie trie;
trie.insert("cat");
trie.insert("car");
trie.insert("dog");
trie.printTrie(trie.root, "");
return 0;
}Attempts:
2 left
💡 Hint
Think about how the printTrie function recursively prints words in alphabetical order.
✗ Incorrect
The printTrie function prints words in lexicographical order because it recursively visits children from 'a' to 'z'. The words inserted are "cat", "car", and "dog". Among "car" and "cat", "car" comes first alphabetically, then "cat", then "dog".
❓ Predict Output
intermediate1:30remaining
Number of nodes after inserting words in Trie
After inserting the words
"apple" and "app" into an empty Trie, how many Trie nodes are created in total?DSA C++
struct TrieNode {
TrieNode* children[26] = {nullptr};
bool isEnd = false;
};
class Trie {
public:
TrieNode* root;
int nodeCount;
Trie() {
root = new TrieNode();
nodeCount = 1; // root node
}
void insert(const std::string& word) {
TrieNode* node = root;
for (char c : word) {
int index = c - 'a';
if (!node->children[index]) {
node->children[index] = new TrieNode();
nodeCount++;
}
node = node->children[index];
}
node->isEnd = true;
}
};
int main() {
Trie trie;
trie.insert("apple");
trie.insert("app");
std::cout << trie.nodeCount << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Count nodes created for each unique character. Shared prefixes reuse nodes.
✗ Incorrect
Inserting "apple" creates nodes for 'a', 'p', 'p', 'l', 'e' plus the root node = 6 nodes. Inserting "app" reuses 'a', 'p', 'p' nodes, so no new nodes are added. Total nodes = 6.
🔧 Debug
advanced1:30remaining
Identify the bug in Trie insert method
What error will occur when running this Trie insert method code snippet?
DSA C++
void insert(std::string word) {
TrieNode* node = root;
for (int i = 0; i <= word.length(); i++) {
int index = word[i] - 'a';
if (!node->children[index]) {
node->children[index] = new TrieNode();
}
node = node->children[index];
}
node->isEnd = true;
}Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The loop runs from i = 0 to i <= word.length(), which means it accesses word[word.length()] on last iteration. This is out-of-bounds and causes runtime error.
🧠 Conceptual
advanced1:00remaining
Trie insert operation complexity
What is the time complexity of inserting a word of length
n into a Trie?Attempts:
2 left
💡 Hint
Consider how many characters you process during insertion.
✗ Incorrect
Insertion processes each character once, so time complexity is linear in the word length, O(n).
🚀 Application
expert2:00remaining
Result of inserting overlapping words in Trie
Given an empty Trie, after inserting the words
"bat", "bath", and "batman", which of the following words will be recognized as complete words in the Trie?Attempts:
2 left
💡 Hint
Check which nodes have the isEnd flag set after all insertions.
✗ Incorrect
Each inserted word sets isEnd at its last character node. All three words are inserted fully, so all three are recognized as complete words.