0
0
DSA Javascriptprogramming~10 mins

Autocomplete System with 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
class TrieNode {
  constructor() {
    this.children = [1];
    this.isEndOfWord = false;
  }
}
Drag options to blanks, or click blank then click option'
A{}
Bnull
C[]
D''
Attempts:
3 left
💡 Hint
Common Mistakes
Using an array [] instead of an object for children.
Setting children to null or empty string.
2fill in blank
medium

Complete the code to insert a character into the Trie children if it does not exist.

DSA Javascript
insert(word) {
  let node = this.root;
  for (let char of word) {
    if (!node.children.hasOwnProperty([1])) {
      node.children[char] = new TrieNode();
    }
    node = node.children[char];
  }
  node.isEndOfWord = true;
}
Drag options to blanks, or click blank then click option'
Achar
Bchildren
Cnode
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' or 'node' instead of 'char' as the key.
Checking children without using hasOwnProperty.
3fill in blank
hard

Fix the error in the search method to correctly check if a word exists in the Trie.

DSA Javascript
search(word) {
  let node = this.root;
  for (let char of word) {
    if (!node.children[[1]]) {
      return false;
    }
    node = node.children[char];
  }
  return node.isEndOfWord;
}
Drag options to blanks, or click blank then click option'
Anode
Bword
Cchar
Dchildren
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' or 'node' instead of 'char' as the key.
Checking children without proper key.
4fill in blank
hard

Fill both blanks to complete the DFS helper function that collects all words starting from a node.

DSA Javascript
dfs(node, prefix, results) {
  if (node.isEndOfWord) {
    results.push([1]);
  }
  for (let char in node.children) {
    this.dfs(node.children[char], prefix [2] char, results);
  }
}
Drag options to blanks, or click blank then click option'
Aprefix
Bprefix + char
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing the wrong variable instead of prefix.
Using '-' instead of '+' for string concatenation.
5fill in blank
hard

Fill the blanks to complete the autocomplete method that returns all words starting with a prefix.

DSA Javascript
autocomplete(prefix) {
  let node = this.root;
  for (let char of prefix) {
    if (!node.children[[1]]) {
      return [];
    }
    node = node.children[char];
  }
  let results = [];
  this.dfs(node, [2], results);
  return results;
}
Drag options to blanks, or click blank then click option'
Aprefix
Bchar
D''
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prefix' instead of 'char' in the loop check.
Passing empty string instead of prefix to dfs.