Consider you want to find all words starting with a given prefix in a large dictionary. Why is a Trie data structure more efficient than a Hash Map for this task?
Think about how each data structure organizes keys and how that affects searching by prefix.
Trie organizes strings by their characters in a tree, so searching for a prefix means following the path of those characters. Hash Map stores whole keys without order, so it cannot efficiently find all keys sharing a prefix without checking all keys.
What is the output of the following C++ code that inserts words into a Trie and searches for a prefix count?
struct TrieNode {
int count = 0;
TrieNode* children[26] = {nullptr};
};
class Trie {
TrieNode* root;
public:
Trie() { root = new TrieNode(); }
void insert(const std::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->count++;
}
}
int prefixCount(const std::string& prefix) {
TrieNode* node = root;
for (char c : prefix) {
int idx = c - 'a';
if (!node->children[idx]) return 0;
node = node->children[idx];
}
return node->count;
}
};
int main() {
Trie trie;
trie.insert("apple");
trie.insert("app");
trie.insert("april");
int count = trie.prefixCount("app");
std::cout << count << std::endl;
return 0;
}Count how many inserted words start with "app".
Words "apple" and "app" start with "app". "april" starts with "ap" but not "app". The node after traversing "app" (after the second 'p') has count=2, because only those two words pass through it. "april" passes through up to the node after the first 'p', incrementing it to 3, but not the second 'p'.
What error will this C++ code produce when searching for a prefix in a Trie?
int prefixCount(const std::string& prefix) { TrieNode* node = root; for (char c : prefix) { int idx = c - 'a'; if (node->children[idx] == nullptr) return 0; node = node->children[idx]; } return node->count; } // root is not initialized in constructor
Check if root pointer is properly set before usage.
If root is not initialized (e.g., not assigned new TrieNode()), accessing root->children causes a segmentation fault (crash) because root points to garbage.
Choose the correct syntax to declare an array of 26 TrieNode pointers named children inside a struct.
Remember how to declare an array of pointers in C++.
Option D declares an array of 26 pointers to TrieNode. Option D declares an array of TrieNode objects (not pointers). Option D tries to assign a pointer to an array but is incorrect syntax for member declaration. Option D is invalid syntax.
You want to build an autocomplete feature that suggests words as a user types. Why is a Trie better suited than a Hash Map for this?
Think about how each data structure organizes keys and how autocomplete needs to find all keys starting with a prefix.
Trie stores words by characters in a tree, so it can quickly find all words sharing a prefix by traversing the tree. Hash Map stores whole keys without order, so it cannot efficiently find all keys with a prefix without scanning all keys.