Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Searching a Word in Trie
What is the output of the following code that inserts words into a Trie and searches for a specific word?
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('car'));
Attempts:
2 left
💡 Hint
Check if the word 'car' was inserted and if the search method returns true only for complete words.
✗ Incorrect
The word 'car' was inserted into the Trie. The search method returns true only if the word exists and the last node marks the end of a word.
🧠 Conceptual
intermediate1:30remaining
Understanding Trie Node Structure
Which of the following best describes the role of the 'isEndOfWord' property in a Trie node?
Attempts:
2 left
💡 Hint
Think about how the Trie knows when a word ends.
✗ Incorrect
The 'isEndOfWord' boolean indicates that the path to this node forms a complete word stored in the Trie.
🔧 Debug
advanced2:00remaining
Identify the Error in Trie Search Method
What error will occur when running this code snippet that searches for a word in a Trie?
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('apple'); console.log(trie.search('app'));
Attempts:
2 left
💡 Hint
Check if 'app' was inserted as a complete word or just a prefix.
✗ Incorrect
The word 'app' was not inserted as a complete word, only 'apple' was. So search returns false.
🚀 Application
advanced2:00remaining
Result of Inserting and Searching Multiple Words
After inserting the words ['bat', 'bath', 'batman', 'batmobile'] into an empty Trie, what will be the output of searching for 'batm'?
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(); ['bat', 'bath', 'batman', 'batmobile'].forEach(word => trie.insert(word)); console.log(trie.search('batm'));
Attempts:
2 left
💡 Hint
Check if 'batm' was inserted as a complete word or just a prefix.
✗ Incorrect
'batm' is a prefix of 'batman' and 'batmobile' but not a complete word itself, so search returns false.
🧠 Conceptual
expert2:30remaining
Trie Memory Usage and Optimization
Which of the following statements about Trie memory usage and optimization is correct?
Attempts:
2 left
💡 Hint
Think about how nodes with only one child can be merged to save space.
✗ Incorrect
Compressed Tries (or Radix Trees) merge chains of single-child nodes to reduce memory usage.