0
0
DSA C++programming~20 mins

Autocomplete System with 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 <string>
using namespace std;

struct TrieNode {
    TrieNode* children[26] = {nullptr};
    bool isEnd = false;
};

class Trie {
public:
    TrieNode* root;
    Trie() { root = new TrieNode(); }

    void insert(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->isEnd = true;
    }

    bool startsWith(string prefix) {
        TrieNode* node = root;
        for (char c : prefix) {
            int idx = c - 'a';
            if (!node->children[idx]) return false;
            node = node->children[idx];
        }
        return true;
    }
};

int main() {
    Trie trie;
    trie.insert("apple");
    trie.insert("app");
    cout << trie.startsWith("app") << endl;
    cout << trie.startsWith("apl") << endl;
    return 0;
}
A
1
0
B
0
1
C
1
1
D
0
0
Attempts:
2 left
💡 Hint
Check if the prefix exists in the Trie after inserting the words.
🧠 Conceptual
intermediate
1:30remaining
Understanding Trie Node Structure
Which of the following best describes the purpose of the 'isEnd' boolean in a Trie node?
AIt counts how many words pass through this node.
BIt marks the node as the end of a valid word stored in the Trie.
CIt stores the character value of the node.
DIt indicates if the node has any children nodes.
Attempts:
2 left
💡 Hint
Think about how the Trie knows when a word finishes.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Trie Search
What error will occur when running this code snippet for searching a word in a Trie?
DSA C++
bool search(string word) {
    TrieNode* node = root;
    for (char c : word) {
        int idx = c - 'a';
        if (node->children[idx] == nullptr)
            return false;
        node = node->children[idx];
    }
    return node->isEnd;
}

// root is not initialized in constructor
AReturns false always
BRuns correctly and returns true or false
CCompilation error due to missing return statement
DSegmentation fault due to uninitialized root pointer
Attempts:
2 left
💡 Hint
Check if root pointer is properly set before use.
🚀 Application
advanced
2:30remaining
Autocomplete Suggestions Using Trie
Given a Trie with words inserted, which approach correctly collects all words starting with a given prefix?
AStore all words in a list and search prefix using string matching
BCheck all nodes in Trie and filter words starting with prefix
CTraverse to prefix node, then DFS to collect all words from there
DUse BFS from root to find prefix and return first matching word only
Attempts:
2 left
💡 Hint
Think about efficient traversal after locating prefix node.
Predict Output
expert
3:00remaining
Output of Trie Autocomplete Function
What is the output of this C++ code that inserts words and prints autocomplete suggestions for prefix "ca"?
DSA C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct TrieNode {
    TrieNode* children[26] = {nullptr};
    bool isEnd = false;
};

class Trie {
public:
    TrieNode* root;
    Trie() { root = new TrieNode(); }

    void insert(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->isEnd = true;
    }

    void dfs(TrieNode* node, string prefix, vector<string>& results) {
        if (node->isEnd) results.push_back(prefix);
        for (int i = 0; i < 26; i++) {
            if (node->children[i]) {
                dfs(node->children[i], prefix + char('a' + i), results);
            }
        }
    }

    vector<string> autocomplete(string prefix) {
        TrieNode* node = root;
        for (char c : prefix) {
            int idx = c - 'a';
            if (!node->children[idx]) return {};
            node = node->children[idx];
        }
        vector<string> results;
        dfs(node, prefix, results);
        return results;
    }
};

int main() {
    Trie trie;
    trie.insert("cat");
    trie.insert("car");
    trie.insert("cart");
    trie.insert("dog");
    vector<string> res = trie.autocomplete("ca");
    for (string s : res) cout << s << " ";
    return 0;
}
Acar cart cat
Bcart car cat
Ccat car cart
Ddog
Attempts:
2 left
💡 Hint
Check the order of DFS traversal over children nodes.