Challenge - 5 Problems
Prefix Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Prefix Search Result
What is the output of the following code that inserts words into a Trie and searches for words with prefix 'app'?
DSA Javascript
class TrieNode { constructor() { this.children = {}; this.isEnd = 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.isEnd = true; } startsWith(prefix) { let node = this.root; for (const char of prefix) { if (!node.children[char]) { return []; } node = node.children[char]; } const results = []; const dfs = (currNode, path) => { if (currNode.isEnd) results.push(path); for (const c in currNode.children) { dfs(currNode.children[c], path + c); } }; dfs(node, prefix); return results; } } const trie = new Trie(); ['apple', 'app', 'application', 'bat', 'ball'].forEach(w => trie.insert(w)); console.log(trie.startsWith('app'));
Attempts:
2 left
💡 Hint
Think about all words starting exactly with 'app' including the prefix itself.
✗ Incorrect
The Trie stores all words. The startsWith method finds all words starting with 'app'. The words 'app', 'apple', and 'application' start with 'app'.
🧠 Conceptual
intermediate1:30remaining
Number of Nodes in Trie After Insertions
After inserting the words ['cat', 'car', 'cart', 'dog'] into an empty Trie, how many nodes does the Trie contain?
Attempts:
2 left
💡 Hint
Count shared prefixes only once.
✗ Incorrect
The words share prefixes: 'ca' is shared by 'cat', 'car', 'cart'. Counting nodes: root + c + a + t + r + t + d + o + g = 9 nodes.
🔧 Debug
advanced1:30remaining
Identify the Error in Trie Search Method
What error does the following Trie search method produce when searching for a prefix that does not exist?
DSA Javascript
startsWith(prefix) {
let node = this.root;
for (const char of prefix) {
node = node.children[char];
}
return node !== undefined;
}Attempts:
2 left
💡 Hint
What happens if node.children[char] is undefined and you try to access its children?
✗ Incorrect
If prefix character is missing, node.children[char] is undefined. Next loop tries to access children of undefined causing TypeError.
🚀 Application
advanced1:30remaining
Find All Words with Prefix in Trie
Given a Trie with words inserted, which method correctly returns all words starting with a given prefix?
Attempts:
2 left
💡 Hint
Think about efficient search using Trie structure.
✗ Incorrect
The efficient way is to find the node representing prefix, then explore all descendants to get words starting with prefix.
❓ Predict Output
expert2:30remaining
Output of Complex Trie Prefix Search
What is the output of the following code snippet?
DSA Javascript
class TrieNode { constructor() { this.children = {}; this.isEnd = 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.isEnd = true; } startsWith(prefix) { let node = this.root; for (const char of prefix) { if (!node.children[char]) return []; node = node.children[char]; } const results = []; const dfs = (currNode, path) => { if (currNode.isEnd) results.push(path); for (const c in currNode.children) { dfs(currNode.children[c], path + c); } }; dfs(node, prefix); return results; } } const trie = new Trie(); ['team', 'teach', 'test', 'tester', 'testing'].forEach(w => trie.insert(w)); console.log(trie.startsWith('te'));
Attempts:
2 left
💡 Hint
All words starting with 'te' should be included.
✗ Incorrect
All inserted words start with 'te', so all are returned in lexicographical order of DFS traversal.