0
0
DSA Javascriptprogramming~10 mins

Word Search in 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 create a new Trie node with an empty children object.

DSA Javascript
function createNode() {
  return { children: [1] };
}
Drag options to blanks, or click blank then click option'
Anull
B[]
C{}
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Using an array [] instead of an object for children.
2fill in blank
medium

Complete the code to check if a character exists in the current Trie node's children.

DSA Javascript
if (node.children.hasOwnProperty([1])) {
  // character exists
}
Drag options to blanks, or click blank then click option'
Achar
B'char'
Cnode
Dchildren
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal 'char' instead of the variable char.
3fill in blank
hard

Fix the error in the search function to return true if the word is found in the Trie.

DSA Javascript
function search(word) {
  let node = root;
  for (let char of word) {
    if (!node.children.hasOwnProperty(char)) {
      return false;
    }
    node = node.children[[1]];
  }
  return node.isEnd === true;
}
Drag options to blanks, or click blank then click option'
Aword
Bchar
Cnode
Dchildren
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' or 'node' instead of 'char' as the key.
4fill in blank
hard

Fill both blanks to insert a word into the Trie correctly.

DSA Javascript
function insert(word) {
  let node = root;
  for (let char of word) {
    if (!node.children.hasOwnProperty([1])) {
      node.children[[2]] = createNode();
    }
    node = node.children[char];
  }
  node.isEnd = true;
}
Drag options to blanks, or click blank then click option'
Achar
Bword
Cnode
Dchildren
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables like 'word' or 'node' instead of 'char'.
5fill in blank
hard

Fill all three blanks to create a function that searches for a prefix in the Trie.

DSA Javascript
function startsWith(prefix) {
  let node = root;
  for (let [1] of prefix) {
    if (!node.children.hasOwnProperty([2])) {
      return false;
    }
    node = node.children[[3]];
  }
  return true;
}
Drag options to blanks, or click blank then click option'
Achar
Bprefix
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prefix' or 'node' instead of 'char' for keys.