Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after inserting words into a Trie?
Consider a Trie data structure where each node stores children in an object and a boolean flag for end of word. After inserting the words "cat" and "car", what is the printed structure of the root node's children keys?
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; } } const trie = new Trie(); trie.insert("cat"); trie.insert("car"); console.log(Object.keys(trie.root.children));
Attempts:
2 left
💡 Hint
Think about the first letter of each inserted word and how the root node stores children.
✗ Incorrect
The root node only stores the first letters of inserted words as keys in its children object. Both "cat" and "car" start with 'c', so the root's children keys contain only ['c'].
❓ Predict Output
intermediate2:00remaining
What is the value of isEndOfWord after inserting 'dog' and 'dot'?
After inserting the words "dog" and "dot" into an empty Trie, what is the value of
isEndOfWord for the node representing the letter 'g' in "dog"?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; } } const trie = new Trie(); trie.insert("dog"); trie.insert("dot"); console.log(trie.root.children['d'].children['o'].children['g'].isEndOfWord);
Attempts:
2 left
💡 Hint
Check if the last character node of the inserted word marks the end of a word.
✗ Incorrect
The node for 'g' in "dog" is the last character node of the word "dog", so its isEndOfWord is set to true during insertion.
❓ Predict Output
advanced2:00remaining
What is the output after inserting overlapping words in a Trie?
Given the following insertions into a Trie: "bat", "bath", and "baton", what is the value of
isEndOfWord for the node representing 't' in "bat"?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; } } const trie = new Trie(); trie.insert("bat"); trie.insert("bath"); trie.insert("baton"); console.log(trie.root.children['b'].children['a'].children['t'].isEndOfWord);
Attempts:
2 left
💡 Hint
Remember that each inserted word marks the end of its last character node.
✗ Incorrect
The node for 't' in "bat" is the end of the word "bat", so its isEndOfWord is true even though longer words share the prefix.
🧠 Conceptual
advanced2:00remaining
How many nodes are created after inserting 'apple' and 'app' into an empty Trie?
If you insert the words "apple" and "app" into an empty Trie, how many total nodes (including the root) will the Trie have?
Attempts:
2 left
💡 Hint
Count nodes for unique characters only; shared prefixes reuse nodes.
✗ Incorrect
The root node plus nodes for 'a', 'p', 'p', 'l', 'e' total 6 nodes. Since "app" shares the first three letters, no new nodes are added for it. So total nodes = 1 (root) + 5 = 6 nodes. But careful: root + a + p + p + l + e = 6 nodes total. So answer is 6.
❓ Predict Output
expert2:00remaining
What is the output after inserting words and checking a non-existent path?
After inserting "hello" and "helium" into a Trie, what is the output of accessing
trie.root.children['h'].children['e'].children['l'].children['x']?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; } } const trie = new Trie(); trie.insert("hello"); trie.insert("helium"); console.log(trie.root.children['h'].children['e'].children['l'].children['x']);
Attempts:
2 left
💡 Hint
Check if the path for 'x' exists under the given nodes.
✗ Incorrect
The path for 'x' does not exist under the node for 'l', so accessing children['x'] returns undefined.