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 Insert and Search
What is the output of the following C++ code that inserts words into a Trie and searches for a prefix?
DSA C++
#include <iostream> #include <string> using namespace std; struct TrieNode { TrieNode* children[26] = {nullptr}; bool isEnd = false; }; class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } void insert(string word) { TrieNode* node = root; for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new TrieNode(); node = node->children[idx]; } node->isEnd = true; } bool startsWith(string prefix) { TrieNode* node = root; for (char c : prefix) { int idx = c - 'a'; if (!node->children[idx]) return false; node = node->children[idx]; } return true; } }; int main() { Trie trie; trie.insert("apple"); trie.insert("app"); cout << trie.startsWith("app") << endl; cout << trie.startsWith("apl") << endl; return 0; }
Attempts:
2 left
💡 Hint
Check if the prefix exists in the Trie after inserting the words.
✗ Incorrect
The prefix "app" exists because both "app" and "apple" were inserted, so startsWith("app") returns true (1). The prefix "apl" does not exist, so startsWith("apl") returns false (0).
🧠 Conceptual
intermediate1:30remaining
Understanding Trie Node Structure
Which of the following best describes the purpose of the 'isEnd' boolean in a Trie node?
Attempts:
2 left
💡 Hint
Think about how the Trie knows when a word finishes.
✗ Incorrect
The 'isEnd' boolean marks that the path from root to this node forms a complete valid word in the Trie.
🔧 Debug
advanced2:00remaining
Identify the Bug in Trie Search
What error will occur when running this code snippet for searching a word in a Trie?
DSA C++
bool search(string word) { TrieNode* node = root; for (char c : word) { int idx = c - 'a'; if (node->children[idx] == nullptr) return false; node = node->children[idx]; } return node->isEnd; } // root is not initialized in constructor
Attempts:
2 left
💡 Hint
Check if root pointer is properly set before use.
✗ Incorrect
If root is not initialized (e.g., root = new TrieNode()), accessing root->children causes a segmentation fault (crash).
🚀 Application
advanced2:30remaining
Autocomplete 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 after locating prefix node.
✗ Incorrect
After reaching the node representing the prefix, a depth-first search (DFS) collects all words below it efficiently.
❓ Predict Output
expert3:00remaining
Output of Trie Autocomplete Function
What is the output of this C++ code that inserts words and prints autocomplete suggestions for prefix "ca"?
DSA C++
#include <iostream> #include <vector> #include <string> using namespace std; struct TrieNode { TrieNode* children[26] = {nullptr}; bool isEnd = false; }; class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } void insert(string word) { TrieNode* node = root; for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new TrieNode(); node = node->children[idx]; } node->isEnd = true; } void dfs(TrieNode* node, string prefix, vector<string>& results) { if (node->isEnd) results.push_back(prefix); for (int i = 0; i < 26; i++) { if (node->children[i]) { dfs(node->children[i], prefix + char('a' + i), results); } } } vector<string> autocomplete(string prefix) { TrieNode* node = root; for (char c : prefix) { int idx = c - 'a'; if (!node->children[idx]) return {}; node = node->children[idx]; } vector<string> results; dfs(node, prefix, results); return results; } }; int main() { Trie trie; trie.insert("cat"); trie.insert("car"); trie.insert("cart"); trie.insert("dog"); vector<string> res = trie.autocomplete("ca"); for (string s : res) cout << s << " "; return 0; }
Attempts:
2 left
💡 Hint
Check the order of DFS traversal over children nodes.
✗ Incorrect
DFS visits children in alphabetical order (a to z). From prefix "ca", the node has children 'r' (index 17) and 't' (index 19). The loop processes 'r' first, collecting "car" and "cart", then 't', collecting "cat". Thus, the order is "car", "cart", "cat".