0
0
DSA C++programming~20 mins

Trie Insert Operation 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 after inserting words into a Trie
What is the printed output of the Trie structure after inserting the words "cat", "car", and "dog" in this C++ code?
DSA C++
struct TrieNode {
    TrieNode* children[26] = {nullptr};
    bool isEnd = false;
};

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

    void insert(const std::string& word) {
        TrieNode* node = root;
        for (char c : word) {
            int index = c - 'a';
            if (!node->children[index])
                node->children[index] = new TrieNode();
            node = node->children[index];
        }
        node->isEnd = true;
    }

    void printTrie(TrieNode* node, std::string prefix) {
        if (node->isEnd) {
            std::cout << prefix << "\n";
        }
        for (int i = 0; i < 26; i++) {
            if (node->children[i]) {
                printTrie(node->children[i], prefix + char('a' + i));
            }
        }
    }
};

int main() {
    Trie trie;
    trie.insert("cat");
    trie.insert("car");
    trie.insert("dog");
    trie.printTrie(trie.root, "");
    return 0;
}
A"car\ndog\ncat\n"
B"cat\ncar\ndog\n"
C"dog\ncar\ncat\n"
D"car\ncat\ndog\n"
Attempts:
2 left
💡 Hint
Think about how the printTrie function recursively prints words in alphabetical order.
Predict Output
intermediate
1:30remaining
Number of nodes after inserting words in Trie
After inserting the words "apple" and "app" into an empty Trie, how many Trie nodes are created in total?
DSA C++
struct TrieNode {
    TrieNode* children[26] = {nullptr};
    bool isEnd = false;
};

class Trie {
public:
    TrieNode* root;
    int nodeCount;
    Trie() {
        root = new TrieNode();
        nodeCount = 1; // root node
    }

    void insert(const std::string& word) {
        TrieNode* node = root;
        for (char c : word) {
            int index = c - 'a';
            if (!node->children[index]) {
                node->children[index] = new TrieNode();
                nodeCount++;
            }
            node = node->children[index];
        }
        node->isEnd = true;
    }
};

int main() {
    Trie trie;
    trie.insert("apple");
    trie.insert("app");
    std::cout << trie.nodeCount << std::endl;
    return 0;
}
A6
B7
C5
D8
Attempts:
2 left
💡 Hint
Count nodes created for each unique character. Shared prefixes reuse nodes.
🔧 Debug
advanced
1:30remaining
Identify the bug in Trie insert method
What error will occur when running this Trie insert method code snippet?
DSA C++
void insert(std::string word) {
    TrieNode* node = root;
    for (int i = 0; i <= word.length(); i++) {
        int index = word[i] - 'a';
        if (!node->children[index]) {
            node->children[index] = new TrieNode();
        }
        node = node->children[index];
    }
    node->isEnd = true;
}
ALogical error: isEnd flag never set
BRuntime error: accessing out-of-bounds index in string
CCompilation error: missing return type
DNo error, code runs correctly
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
🧠 Conceptual
advanced
1:00remaining
Trie insert operation complexity
What is the time complexity of inserting a word of length n into a Trie?
AO(n), where n is the length of the word
BO(log n), where n is the length of the word
CO(1), constant time
DO(n^2), quadratic time
Attempts:
2 left
💡 Hint
Consider how many characters you process during insertion.
🚀 Application
expert
2:00remaining
Result of inserting overlapping words in Trie
Given an empty Trie, after inserting the words "bat", "bath", and "batman", which of the following words will be recognized as complete words in the Trie?
A["bath", "batman"]
B["bat", "bath"]
C["bat", "bath", "batman"]
D["batman"]
Attempts:
2 left
💡 Hint
Check which nodes have the isEnd flag set after all insertions.