0
0
DSA C++programming~10 mins

Prefix Search Using Trie in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a character into the Trie node's children.

DSA C++
node->children[[1]] = new TrieNode();
Drag options to blanks, or click blank then click option'
A'c' - 'a'
B'b' - 'a'
C'z' - 'a'
D'a' - 'a'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the character directly without subtracting 'a'.
Using wrong character subtraction leading to wrong index.
2fill in blank
medium

Complete the code to check if a prefix exists in the Trie.

DSA C++
if (node->children[[1]] == nullptr) return false;
Drag options to blanks, or click blank then click option'
A'm' - 'a'
B'x' - 'a'
C'p' - 'a'
D'z' - 'a'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong index for the character.
Not checking for nullptr before accessing children.
3fill in blank
hard

Fix the error in the code to mark the end of a word in the Trie.

DSA C++
node->[1] = true;
Drag options to blanks, or click blank then click option'
AisWord
BwordEnd
CendOfWord
DisEnd
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined or inconsistent variable names.
Forgetting to mark the end of word.
4fill in blank
hard

Fill both blanks to complete the loop that traverses the Trie for a prefix.

DSA C++
for (int i = 0; i < [1]; i++) {
    int index = prefix[i] [2] 'a';
    if (node->children[index] == nullptr) return false;
    node = node->children[index];
}
Drag options to blanks, or click blank then click option'
Aprefix.size()
Bprefix.length()
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' for index calculation.
Using wrong length function.
5fill in blank
hard

Fill all three blanks to complete the function that inserts a word into the Trie.

DSA C++
void insert(string word) {
    TrieNode* node = root;
    for (int i = 0; i < [1]; i++) {
        int index = word[i] [2] 'a';
        if (node->children[index] == nullptr) {
            node->children[index] = new TrieNode();
        }
        node = node->children[index];
    }
    node->[3] = true;
}
Drag options to blanks, or click blank then click option'
Aword.size()
B-
CisEnd
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' for index calculation.
Not marking the end of the word.