0
0
DSA C++programming~20 mins

Prefix Search Using Trie in DSA C++ - Practice Problems & Challenges

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!
Predict Output
intermediate
2: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 <unordered_map>
#include <string>
using namespace std;

class TrieNode {
public:
    unordered_map<char, TrieNode*> children;
    bool isEndOfWord;
    TrieNode() : isEndOfWord(false) {}
};

class Trie {
private:
    TrieNode* root;
public:
    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->isEndOfWord = 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 << boolalpha << trie.startsWith("app") << endl;
    cout << boolalpha << trie.startsWith("apl") << endl;
    return 0;
}
A
true
true
B
true
false
C
false
false
D
false
true
Attempts:
2 left
💡 Hint
Think about whether the prefix "app" and "apl" exist in the inserted words.
🧠 Conceptual
intermediate
1:30remaining
Number of Nodes in Trie After Insertions
If you insert the words ["cat", "car", "cart"] into an empty Trie, how many nodes will the Trie contain (including the root)?
A10
B8
C9
D7
Attempts:
2 left
💡 Hint
Count shared prefixes carefully to avoid double counting nodes.
🔧 Debug
advanced
2:00remaining
Identify the Error in Trie Search Function
What error will the following C++ Trie search function cause when searching for a prefix that does not exist?
DSA C++
bool startsWith(const string& prefix) {
    TrieNode* node = root;
    for (char c : prefix) {
        node = node->children[c];
        if (!node) return false;
    }
    return true;
}
ARuntime error: accessing non-existent key in unordered_map
BReturns false correctly without error
CCompilation error due to missing return statement
DAlways returns true regardless of prefix
Attempts:
2 left
💡 Hint
Check how unordered_map operator[] behaves when key is missing.
🚀 Application
advanced
2:30remaining
Find All Words With Given Prefix
Given a Trie with words inserted, which approach correctly finds all words starting with a given prefix?
ATraverse Trie to prefix node, then DFS to collect all words from there
BCheck each word in the Trie root's children for prefix match
CStore all words in a list and filter by prefix each time
DUse BFS from root and stop at first prefix match
Attempts:
2 left
💡 Hint
Think about efficient traversal starting from the prefix node.
Predict Output
expert
3:00remaining
Output of Trie Prefix Search with Multiple Insertions
What is the output of the following C++ code that inserts multiple words and checks prefixes?
DSA C++
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
using namespace std;

class TrieNode {
public:
    unordered_map<char, TrieNode*> children;
    bool isEndOfWord;
    TrieNode() : isEndOfWord(false) {}
};

class Trie {
private:
    TrieNode* root;
    void dfs(TrieNode* node, string current, vector<string>& results) {
        if (node->isEndOfWord) results.push_back(current);
        for (auto& pair : node->children) {
            dfs(pair.second, current + pair.first, results);
        }
    }
public:
    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->isEndOfWord = true;
    }
    vector<string> findWordsWithPrefix(const string& prefix) {
        TrieNode* node = root;
        for (char c : prefix) {
            if (!node->children.count(c)) return {};
            node = node->children[c];
        }
        vector<string> results;
        dfs(node, prefix, results);
        return results;
    }
};

int main() {
    Trie trie;
    trie.insert("test");
    trie.insert("team");
    trie.insert("teach");
    trie.insert("toast");
    vector<string> words = trie.findWordsWithPrefix("te");
    for (const string& w : words) {
        cout << w << " ";
    }
    cout << endl;
    return 0;
}
Atest teach team
Bteach team test
Cteam teach test
Dtest team teach
Attempts:
2 left
💡 Hint
The DFS visits children in order of insertion in unordered_map, which is not ordered, but assume insertion order for this question.