0
0
DSA C++programming~20 mins

Trie Search 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 of Trie Search for Existing Word
What is the output of the following C++ code that inserts words into a Trie and searches for the word "cat"?
DSA C++
#include <iostream>
#include <unordered_map>
using namespace std;

struct TrieNode {
    unordered_map<char, TrieNode*> children;
    bool isEndOfWord = false;
};

class Trie {
public:
    TrieNode* root;
    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 search(const string& word) {
        TrieNode* node = root;
        for (char c : word) {
            if (!node->children.count(c)) return false;
            node = node->children[c];
        }
        return node->isEndOfWord;
    }
};

int main() {
    Trie trie;
    trie.insert("cat");
    trie.insert("car");
    trie.insert("dog");
    cout << (trie.search("cat") ? "Found" : "Not Found") << endl;
    return 0;
}
ARuntime Error
BNot Found
CCompilation Error
DFound
Attempts:
2 left
💡 Hint
Check if the search function correctly identifies the end of the word.
Predict Output
intermediate
2:00remaining
Output of Trie Search for Non-Existing Word
What will be printed when searching for the word "cap" in the Trie after inserting "cat", "car", and "dog"?
DSA C++
#include <iostream>
#include <unordered_map>
using namespace std;

struct TrieNode {
    unordered_map<char, TrieNode*> children;
    bool isEndOfWord = false;
};

class Trie {
public:
    TrieNode* root;
    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 search(const string& word) {
        TrieNode* node = root;
        for (char c : word) {
            if (!node->children.count(c)) return false;
            node = node->children[c];
        }
        return node->isEndOfWord;
    }
};

int main() {
    Trie trie;
    trie.insert("cat");
    trie.insert("car");
    trie.insert("dog");
    cout << (trie.search("cap") ? "Found" : "Not Found") << endl;
    return 0;
}
ANot Found
BFound
CCompilation Error
DRuntime Error
Attempts:
2 left
💡 Hint
Check if the search function returns false when the word is not fully present.
🧠 Conceptual
advanced
1:30remaining
Understanding Trie Search Behavior with Prefixes
If the Trie contains the words "bat" and "bath", what will the search function return when searching for "bat" and "bath" respectively?
A"bat" returns true, "bath" returns false
B"bat" returns true, "bath" returns true
C"bat" returns false, "bath" returns true
D"bat" returns false, "bath" returns false
Attempts:
2 left
💡 Hint
Remember that search returns true only if the word ends at a node marked as end of word.
🔧 Debug
advanced
2:00remaining
Identify the Error in Trie Search Implementation
What error will occur when running this Trie search code snippet?
DSA C++
bool search(const string& word) {
    TrieNode* node = root;
    for (char c : word) {
        if (node->children[c] == nullptr) return false;
        node = node->children[c];
    }
    return node->isEndOfWord;
}
ARuntime Error due to accessing non-existing key in unordered_map
BReturns false always
CCompilation Error due to missing return statement
DWorks correctly without error
Attempts:
2 left
💡 Hint
Check how unordered_map operator[] behaves when key is missing.
🚀 Application
expert
1:30remaining
Number of Words in Trie After Insertions
After inserting the words "apple", "app", "application", "bat", and "bath" into an empty Trie, how many words will the Trie contain?
A3
B4
C5
D6
Attempts:
2 left
💡 Hint
Each inserted word counts as one word regardless of prefix sharing.