0
0
DSA Javascriptprogramming~10 mins

Longest Word in Dictionary Using Trie in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a word into the Trie.

DSA Javascript
insert(word) {
  let node = this.root;
  for (let char of word) {
    if (!node.children.has(char)) {
      node.children.set(char, new TrieNode());
    }
    node = node.[1];
  }
  node.isEnd = true;
}
Drag options to blanks, or click blank then click option'
Achildren.has(char)
Bchildren.set(char)
Cchildren.get(char)
Dchildren.delete(char)
Attempts:
3 left
💡 Hint
Common Mistakes
Using set instead of get to move to the child node.
Checking if child exists but not moving to it.
2fill in blank
medium

Complete the code to check if a word exists in the Trie.

DSA Javascript
search(word) {
  let node = this.root;
  for (let char of word) {
    if (!node.children.has(char)) {
      return false;
    }
    node = node.children.get(char);
  }
  return node[1];
}
Drag options to blanks, or click blank then click option'
A.isWord
B.found
C.exists
D.isEnd
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong property name like isWord or exists.
Returning true without checking if it's the end of a word.
3fill in blank
hard

Fix the error in the DFS function to find the longest word built one character at a time.

DSA Javascript
dfs(node, path) {
  if (!node.isEnd && node !== this.root) return;
  if (path.length > this.longest.length) {
    this.longest = path;
  }
  for (let [char, child] of node.children) {
    this.dfs(child, path + [1]);
  }
}
Drag options to blanks, or click blank then click option'
Achild
Bchar
Cnode
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the child node object instead of the character.
Adding the whole node or path again.
4fill in blank
hard

Fill both blanks to complete the reduce function that builds an object filtering words with length greater than 3 and mapping them to their lengths.

DSA Javascript
const result = words.reduce((acc, word) => {
  if (word.length [2] 3) {
    acc[word][1] word.length;
  }
  return acc;
}, {});
Drag options to blanks, or click blank then click option'
A:
B>
C<
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using colon instead of equals for object property assignment.
Using less than sign for filtering.
5fill in blank
hard

Fill all three blanks to complete the function that finds the longest word in the dictionary using Trie and DFS.

DSA Javascript
function longestWord(words) {
  const trie = new Trie();
  for (const word of words) {
    trie.[1](word);
  }
  trie.longest = "";
  trie.[2](trie.root, "");
  return trie.[3];
}
Drag options to blanks, or click blank then click option'
Ainsert
Bdfs
Clongest
Dsearch
Attempts:
3 left
💡 Hint
Common Mistakes
Calling search instead of insert to add words.
Returning dfs instead of the longest word.