0
0
DSA C++programming~10 mins

Trie vs Hash Map for Prefix Matching in DSA C++ - Interactive Comparison Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a word into the Trie.

DSA C++
void insert(string word) {
    TrieNode* node = root;
    for (char c : word) {
        if (!node->children.count(c)) {
            node->children[c] = new TrieNode();
        }
        node = node->[1];
    }
    node->isEnd = true;
}
Drag options to blanks, or click blank then click option'
Achildren[c]
Bchild[c]
Cchildren.get(c)
Dchild.get(c)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'child' instead of 'children'.
Trying to call get() which is not a C++ map method.
2fill in blank
medium

Complete the code to check if a prefix exists in the Trie.

DSA C++
bool startsWith(string prefix) {
    TrieNode* node = root;
    for (char c : prefix) {
        if (!node->children.count(c)) {
            return false;
        }
        node = node->[1];
    }
    return true;
}
Drag options to blanks, or click blank then click option'
Achildren.get(c)
Bchild[c]
Cchild.get(c)
Dchildren[c]
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong map name 'child'.
Trying to use get() method which is not valid in C++ map.
3fill in blank
hard

Fix the error in the Hash Map prefix search function.

DSA C++
bool hasPrefix(unordered_map<string, bool>& map, string prefix) {
    for (auto& pair : map) {
        if (pair.first.[1](0, prefix.size()) == prefix) {
            return true;
        }
    }
    return false;
}
Drag options to blanks, or click blank then click option'
Asubstr
Bsubstring
Cslice
Dsub
Attempts:
3 left
💡 Hint
Common Mistakes
Using substring() which is a JavaScript or Java method.
Using slice() which is not a C++ string method.
4fill in blank
hard

Fill both blanks to create a Trie search function that returns true if a word exists.

DSA C++
bool search(string word) {
    TrieNode* node = root;
    for (char c : word) {
        if (!node->[1].count(c)) {
            return false;
        }
        node = node->[2];
    }
    return node->isEnd;
}
Drag options to blanks, or click blank then click option'
Achildren
Bchild
Cchildren[c]
Dchild[c]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'child' instead of 'children'.
Trying to access child node without indexing by c.
5fill in blank
hard

Fill all three blanks to build a Hash Map prefix search that returns true if any key starts with the prefix.

DSA C++
bool hasPrefix(unordered_map<string, bool>& map, string prefix) {
    for (auto& [1] : map) {
        if ([1].first.[2](0, prefix.size()) == prefix) {
            return [3];
        }
    }
    return false;
}
Drag options to blanks, or click blank then click option'
Apair
Bsubstr
Ctrue
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'key' as loop variable which is not valid for map iteration.
Using substring or slice instead of substr.
Returning false instead of true when prefix matches.