0
0
DSA C++programming~20 mins

Why Trie Exists and What Hash Map Cannot Do for Strings in DSA C++ - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is a Trie more efficient than a Hash Map for prefix searches?

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?

ATrie stores characters in a tree structure allowing direct traversal of prefixes, while Hash Map requires checking each key individually.
BHash Map stores keys in sorted order, making prefix search faster than Trie.
CTrie uses hashing internally, so it is slower than Hash Map for prefix search.
DHash Map can only store numbers, so it cannot handle string prefixes.
Attempts:
2 left
💡 Hint

Think about how each data structure organizes keys and how that affects searching by prefix.

Predict Output
intermediate
2:00remaining
Output of Trie insertion and prefix search

What is the output of the following C++ code that inserts words into a Trie and searches for a prefix count?

DSA C++
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;
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Count how many inserted words start with "app".

🔧 Debug
advanced
2:00remaining
Identify the error in this Trie prefix search code

What error will this C++ code produce when searching for a prefix in a Trie?

DSA C++
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
AReturns 0 always because prefix not found
BSegmentation fault due to uninitialized root pointer
CCompilation error due to missing return statement
DReturns incorrect count because count is not updated
Attempts:
2 left
💡 Hint

Check if root pointer is properly set before usage.

📝 Syntax
advanced
2:00remaining
Which option correctly declares a TrieNode children array in C++?

Choose the correct syntax to declare an array of 26 TrieNode pointers named children inside a struct.

ATrieNode children[26];
BTrieNode children* [26];
CTrieNode* children = new TrieNode[26];
DTrieNode* children[26];
Attempts:
2 left
💡 Hint

Remember how to declare an array of pointers in C++.

🚀 Application
expert
3:00remaining
Why can't a Hash Map efficiently support autocomplete suggestions?

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?

ATrie uses more memory but cannot find prefixes efficiently compared to Hash Map.
BHash Map stores keys in sorted order, so it is faster than Trie for autocomplete.
CTrie allows traversal of characters to find all words with a prefix, while Hash Map requires checking all keys, which is slow.
DHash Map automatically indexes prefixes, so Trie is unnecessary.
Attempts:
2 left
💡 Hint

Think about how each data structure organizes keys and how autocomplete needs to find all keys starting with a prefix.