Challenge - 5 Problems
Trie Mastery: Longest Word
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Trie Insertion and Search
What is the output of the following code that inserts words into a Trie and searches for a prefix?
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 false; } node = node.children[char]; } return true; } } const trie = new Trie(); trie.insert("apple"); trie.insert("app"); console.log(trie.startsWith("app")); console.log(trie.startsWith("apl"));
Attempts:
2 left
💡 Hint
Think about whether the prefix 'app' exists in the Trie after insertion.
✗ Incorrect
The prefix 'app' exists because both 'app' and 'apple' were inserted. The prefix 'apl' does not exist, so startsWith returns false for it.
🧠 Conceptual
intermediate1:30remaining
Longest Word Construction Condition
In the problem of finding the longest word in a dictionary using a Trie, what condition must be true for a word to be considered valid for the longest word?
Attempts:
2 left
💡 Hint
Think about building the word step by step from smaller words.
✗ Incorrect
The longest word is valid only if all its prefixes are also words in the dictionary, ensuring it can be built one character at a time.
🔧 Debug
advanced2:30remaining
Identify the Bug in Trie Longest Word Search
What error does the following code produce when trying to find the longest word in a Trie?
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;
}
longestWord() {
let result = "";
const dfs = (node, word) => {
if (!node.isEnd && word.length !== 0) return;
if (word.length > result.length || (word.length === result.length && word < result)) {
result = word;
}
for (const char in node.children) {
dfs(node.children[char], word + char);
}
};
dfs(this.root, "");
return result;
}
}
const trie = new Trie();
["w","wo","wor","worl","world"].forEach(w => trie.insert(w));
console.log(trie.longestWord());
Attempts:
2 left
💡 Hint
Check the base case and traversal logic in DFS.
✗ Incorrect
The code correctly returns 'world' as the longest word because all prefixes are inserted and isEnd is checked properly.
🚀 Application
advanced2:00remaining
Longest Word Result After Insertions
Given the following words inserted into a Trie: ["a", "banana", "app", "appl", "ap", "apply", "apple"], what is the longest word returned by the longestWord() method that requires all prefixes to be valid words?
Attempts:
2 left
💡 Hint
Check which words have all prefixes present in the list.
✗ Incorrect
Both "apple" and "apply" have all prefixes present, but "apple" comes before "apply" lexicographically, so it is returned.
🧠 Conceptual
expert2:30remaining
Why Use Trie for Longest Word in Dictionary?
Why is a Trie data structure preferred over sorting and checking prefixes directly when finding the longest word in a dictionary where all prefixes must be valid words?
Attempts:
2 left
💡 Hint
Consider how prefix checks are done repeatedly.
✗ Incorrect
Trie enables fast prefix checks and incremental word building, making it efficient for this problem compared to repeated prefix checks after sorting.