0
0
DSA C++programming~20 mins

Count Words with Given Prefix in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prefix Count Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of prefix count after insertions
What is the output of the following C++ code that inserts words into a prefix count structure and queries the count for prefix "app"?
DSA C++
class TrieNode {
public:
    int count = 0;
    TrieNode* children[26] = {nullptr};
};

class Trie {
public:
    TrieNode* root;
    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 countPrefix(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("application");
    trie.insert("apt");
    std::cout << trie.countPrefix("app") << std::endl;
    return 0;
}
A4
B3
C2
D1
Attempts:
2 left
💡 Hint
Count how many inserted words start with "app".
Predict Output
intermediate
2:00remaining
Count prefix with no matching words
What will be the output of the code when counting prefix "bat" after inserting words "cat", "car", "cart"?
DSA C++
class TrieNode {
public:
    int count = 0;
    TrieNode* children[26] = {nullptr};
};

class Trie {
public:
    TrieNode* root;
    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 countPrefix(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("cat");
    trie.insert("car");
    trie.insert("cart");
    std::cout << trie.countPrefix("bat") << std::endl;
    return 0;
}
A0
B1
C3
D2
Attempts:
2 left
💡 Hint
Check if any inserted word starts with "bat".
🔧 Debug
advanced
2:00remaining
Identify the error in prefix count code
What error will the following code produce when counting prefix "app" after inserting "apple" and "app"?
DSA C++
class TrieNode {
public:
    int count = 0;
    TrieNode* children[26];
    TrieNode() {
        for (int i = 0; i < 26; i++) children[i] = nullptr;
    }
};

class Trie {
public:
    TrieNode* root;
    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 countPrefix(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");
    std::cout << trie.countPrefix("app") << std::endl;
    return 0;
}
ACompilation error: uninitialized array
BRuntime error: segmentation fault
CNo error, output is 2
DOutput is 0
Attempts:
2 left
💡 Hint
Check if children array is initialized properly.
🧠 Conceptual
advanced
2:00remaining
Understanding prefix count updates in Trie
If you insert the words "dog", "door", "dorm", and then delete "door" from a Trie that counts prefixes, what should happen to the prefix counts for "do"?
AThe count for prefix "do" remains the same
BThe count for prefix "do" becomes zero
CThe count for prefix "do" decreases by 2
DThe count for prefix "do" decreases by 1
Attempts:
2 left
💡 Hint
Deleting one word that starts with "do" affects counts accordingly.
🚀 Application
expert
2:00remaining
Find number of words with prefix after multiple insertions
Given the following sequence of insertions into a prefix counting Trie: "test", "tester", "testing", "team", "teach", what is the count of words with prefix "te"?
A5
B3
C2
D4
Attempts:
2 left
💡 Hint
Count all words starting with "te".