0
0
DSA C++programming~20 mins

Trie vs Hash Map for Prefix Matching in DSA C++ - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prefix Matching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Prefix Search Using Trie
What is the output of the following C++ code that inserts words into a Trie and checks if a prefix exists?
DSA C++
#include <iostream>
#include <unordered_map>
using namespace std;

struct TrieNode {
    unordered_map<char, TrieNode*> children;
    bool isEnd = 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->isEnd = true;
    }

    bool startsWith(const string& prefix) {
        TrieNode* node = root;
        for (char c : prefix) {
            if (!node->children.count(c))
                return false;
            node = node->children[c];
        }
        return true;
    }
};

int main() {
    Trie trie;
    trie.insert("apple");
    trie.insert("app");
    cout << (trie.startsWith("app") ? "Yes" : "No") << endl;
    cout << (trie.startsWith("apl") ? "Yes" : "No") << endl;
    return 0;
}
A
Yes
Yes
B
No
Yes
C
Yes
No
D
No
No
Attempts:
2 left
💡 Hint
Think about whether the prefix exists in the inserted words.
🧠 Conceptual
intermediate
1:30remaining
Why Use Trie Over Hash Map for Prefix Matching?
Which of the following is the main advantage of using a Trie over a Hash Map for prefix matching?
AHash Map uses less memory than Trie for prefix matching.
BTrie allows efficient prefix search without storing all prefixes explicitly.
CTrie requires less code to implement than Hash Map.
DHash Map can store prefixes faster than Trie.
Attempts:
2 left
💡 Hint
Think about how prefix search works in both data structures.
Predict Output
advanced
2:00remaining
Output of Prefix Count Using Hash Map
What is the output of this C++ code that counts how many words start with a prefix using a Hash Map?
DSA C++
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
    vector<string> words = {"cat", "car", "cart", "dog"};
    unordered_map<string, int> prefixCount;

    for (const string& word : words) {
        for (int i = 1; i <= word.size(); ++i) {
            prefixCount[word.substr(0, i)]++;
        }
    }

    cout << prefixCount["car"] << endl;
    cout << prefixCount["ca"] << endl;
    cout << prefixCount["do"] << endl;
    cout << prefixCount["c"] << endl;
    return 0;
}
A
1
2
1
2
B
2
2
1
2
C
3
3
1
3
D
2
3
1
3
Attempts:
2 left
💡 Hint
Count how many words start with each prefix.
🔧 Debug
advanced
2:00remaining
Identify the Error in Trie Prefix Search
What error will this C++ code produce when checking prefix existence in a Trie?
DSA C++
#include <iostream>
#include <unordered_map>
using namespace std;

struct TrieNode {
    unordered_map<char, TrieNode*> children;
    bool isEnd = 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[c])
                node->children[c] = new TrieNode();
            node = node->children[c];
        }
        node->isEnd = true;
    }

    bool startsWith(const string& prefix) {
        TrieNode* node = root;
        for (char c : prefix) {
            if (node->children.count(c) == 0)
                return false;
            node = node->children[c];
        }
        return true;
    }
};

int main() {
    Trie trie;
    trie.insert("hello");
    cout << (trie.startsWith("hel") ? "Yes" : "No") << endl;
    return 0;
}
ARuntime error due to accessing map with operator[] on missing key
BNo error, outputs Yes
CAlways returns false for any prefix
DCompilation error due to missing semicolon
Attempts:
2 left
💡 Hint
Check how operator[] behaves on unordered_map when key is missing.
🧠 Conceptual
expert
2:30remaining
Memory Efficiency Comparison Between Trie and Hash Map for Prefix Matching
Which statement best describes memory usage when storing a large dictionary for prefix matching using Trie vs Hash Map?
ATrie uses less memory because it shares common prefixes, while Hash Map stores all prefixes separately.
BHash Map uses less memory because it stores only full words as keys, not prefixes.
CTrie and Hash Map use about the same memory for prefix matching.
DHash Map uses less memory because it compresses prefixes automatically.
Attempts:
2 left
💡 Hint
Think about how prefixes are stored and shared in each data structure.