Challenge - 5 Problems
Trie Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of the Trie search for 'cat'?
Given the following Trie implementation and insertions, what will be the output of searching for the word 'cat'?
DSA Javascript
class TrieNode { constructor() { this.children = {}; this.isEndOfWord = false; } } class Trie { constructor() { this.root = new TrieNode(); } insert(word) { let node = this.root; for (const char of word) { if (!node.children[char]) { node.children[char] = new TrieNode(); } node = node.children[char]; } node.isEndOfWord = true; } search(word) { let node = this.root; for (const char of word) { if (!node.children[char]) { return false; } node = node.children[char]; } return node.isEndOfWord; } } const trie = new Trie(); trie.insert('cat'); trie.insert('car'); trie.insert('dog'); console.log(trie.search('cat'));
Attempts:
2 left
💡 Hint
Check if the search method returns true only when the word is fully present and marked as end of word.
✗ Incorrect
The word 'cat' was inserted into the Trie and marked as an end of a word. The search method returns true when the full word is found and the last node is marked as end of word.
❓ Predict Output
intermediate2:00remaining
What is the output of searching for 'ca' in the Trie?
Using the same Trie from the previous example, what will be the output of searching for the word 'ca'?
DSA Javascript
console.log(trie.search('ca'));Attempts:
2 left
💡 Hint
Remember that 'ca' is a prefix but not a complete word inserted.
✗ Incorrect
The search method returns false because 'ca' is only a prefix in the Trie, not marked as an end of a word.
🧠 Conceptual
advanced2:00remaining
Which statement about Trie search operation is correct?
Choose the correct statement about how the search operation works in a Trie data structure.
Attempts:
2 left
💡 Hint
Think about what makes a word complete in a Trie.
✗ Incorrect
A Trie search returns true only if the entire word is found character by character and the last character node is marked as the end of a word.
🔧 Debug
advanced2:00remaining
What error occurs when searching for a word with a missing character node?
Consider the Trie search method. What happens if the search word contains a character not present in the Trie nodes?
DSA Javascript
search(word) {
let node = this.root;
for (const char of word) {
if (!node.children[char]) {
return false;
}
node = node.children[char];
}
return node.isEndOfWord;
}Attempts:
2 left
💡 Hint
Check the condition when a character node is missing.
✗ Incorrect
The search method returns false immediately when a character node is missing, indicating the word is not present.
🚀 Application
expert3:00remaining
How many words are found after inserting and searching in the Trie?
After inserting the words ['apple', 'app', 'application', 'bat', 'bath'] into an empty Trie, how many of these words will return true when searched?
DSA Javascript
const trie = new Trie(); ['apple', 'app', 'application', 'bat', 'bath'].forEach(word => trie.insert(word)); const results = ['apple', 'app', 'application', 'bat', 'bath'].map(word => trie.search(word)); const count = results.filter(Boolean).length; console.log(count);
Attempts:
2 left
💡 Hint
All inserted words are marked as end of word nodes.
✗ Incorrect
All inserted words are present and marked as complete words, so all searches return true, count is 5.