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 of Trie Search for Existing Word
What is the output of the following C++ code that inserts words into a Trie and searches for the word "cat"?
DSA C++
#include <iostream> #include <unordered_map> using namespace std; struct TrieNode { unordered_map<char, TrieNode*> children; bool isEndOfWord = false; }; class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } void insert(const string& word) { TrieNode* node = root; for (char c : word) { if (!node->children.count(c)) { node->children[c] = new TrieNode(); } node = node->children[c]; } node->isEndOfWord = true; } bool search(const string& word) { TrieNode* node = root; for (char c : word) { if (!node->children.count(c)) return false; node = node->children[c]; } return node->isEndOfWord; } }; int main() { Trie trie; trie.insert("cat"); trie.insert("car"); trie.insert("dog"); cout << (trie.search("cat") ? "Found" : "Not Found") << endl; return 0; }
Attempts:
2 left
💡 Hint
Check if the search function correctly identifies the end of the word.
✗ Incorrect
The word "cat" was inserted into the Trie, so searching for it returns true and prints "Found".
❓ Predict Output
intermediate2:00remaining
Output of Trie Search for Non-Existing Word
What will be printed when searching for the word "cap" in the Trie after inserting "cat", "car", and "dog"?
DSA C++
#include <iostream> #include <unordered_map> using namespace std; struct TrieNode { unordered_map<char, TrieNode*> children; bool isEndOfWord = false; }; class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } void insert(const string& word) { TrieNode* node = root; for (char c : word) { if (!node->children.count(c)) { node->children[c] = new TrieNode(); } node = node->children[c]; } node->isEndOfWord = true; } bool search(const string& word) { TrieNode* node = root; for (char c : word) { if (!node->children.count(c)) return false; node = node->children[c]; } return node->isEndOfWord; } }; int main() { Trie trie; trie.insert("cat"); trie.insert("car"); trie.insert("dog"); cout << (trie.search("cap") ? "Found" : "Not Found") << endl; return 0; }
Attempts:
2 left
💡 Hint
Check if the search function returns false when the word is not fully present.
✗ Incorrect
The word "cap" was not inserted, so the search returns false and prints "Not Found".
🧠 Conceptual
advanced1:30remaining
Understanding Trie Search Behavior with Prefixes
If the Trie contains the words "bat" and "bath", what will the search function return when searching for "bat" and "bath" respectively?
Attempts:
2 left
💡 Hint
Remember that search returns true only if the word ends at a node marked as end of word.
✗ Incorrect
Both "bat" and "bath" were inserted, so searching for either returns true.
🔧 Debug
advanced2:00remaining
Identify the Error in Trie Search Implementation
What error will occur when running this Trie search code snippet?
DSA C++
bool search(const string& word) { TrieNode* node = root; for (char c : word) { if (node->children[c] == nullptr) return false; node = node->children[c]; } return node->isEndOfWord; }
Attempts:
2 left
💡 Hint
Check how unordered_map operator[] behaves when key is missing.
✗ Incorrect
Using operator[] on unordered_map with a missing key inserts a new key with default value, so checking == nullptr is invalid and can cause logic errors or runtime issues.
🚀 Application
expert1:30remaining
Number of Words in Trie After Insertions
After inserting the words "apple", "app", "application", "bat", and "bath" into an empty Trie, how many words will the Trie contain?
Attempts:
2 left
💡 Hint
Each inserted word counts as one word regardless of prefix sharing.
✗ Incorrect
All five words are distinct and inserted, so the Trie contains 5 words.