0
0
DSA Javascriptprogramming~20 mins

Longest Word in Dictionary Using Trie in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trie Mastery: Longest Word
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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"));
Atrue\nfalse
Btrue\ntrue
Cfalse\nfalse
Dfalse\ntrue
Attempts:
2 left
💡 Hint
Think about whether the prefix 'app' exists in the Trie after insertion.
🧠 Conceptual
intermediate
1: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?
AThe word must not share any prefix with other words
BThe word must be the shortest in the dictionary
CEvery prefix of the word must also be a word in the dictionary
DThe word must contain at least one vowel
Attempts:
2 left
💡 Hint
Think about building the word step by step from smaller words.
🔧 Debug
advanced
2: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());
ATypeError: Cannot read property 'isEnd' of undefined
BSyntaxError: Unexpected token
CReturns empty string ""
DReturns "world" correctly
Attempts:
2 left
💡 Hint
Check the base case and traversal logic in DFS.
🚀 Application
advanced
2: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?
A"apple"
B"appl"
C"apply"
D"banana"
Attempts:
2 left
💡 Hint
Check which words have all prefixes present in the list.
🧠 Conceptual
expert
2: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?
ASorting is always slower than Trie insertion
BTrie allows efficient prefix checking and incremental building of words in O(N * L) time where N is number of words and L is max length
CTrie uses less memory than arrays or hash maps
DTrie automatically sorts words lexicographically without extra code
Attempts:
2 left
💡 Hint
Consider how prefix checks are done repeatedly.