0
0
DSA Javascriptprogramming~20 mins

Autocomplete System with Trie in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trie Autocomplete Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of inserting words into a Trie
What is the output of the following code that inserts words into a Trie and then prints the children of the root node?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isEndOfWord = 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.isEndOfWord = true;
  }
}

const trie = new Trie();
trie.insert('cat');
trie.insert('car');
trie.insert('dog');
console.log(Object.keys(trie.root.children));
A['c', 'd']
B['cat', 'car', 'dog']
C['c', 'a', 'd']
D['car', 'dog']
Attempts:
2 left
💡 Hint
Look at the first letters of the inserted words and what keys the root node holds.
Predict Output
intermediate
2:00remaining
Output of searching a prefix in a Trie
What does the following code print when searching for the prefix 'ca' in the Trie?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isEndOfWord = 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.isEndOfWord = 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('cat');
trie.insert('car');
trie.insert('dog');
console.log(trie.startsWith('ca'));
Aundefined
Bfalse
Ctrue
DTypeError
Attempts:
2 left
💡 Hint
Check if the prefix 'ca' exists in the inserted words.
🔧 Debug
advanced
3:00remaining
Find the error in the autocomplete function
The following autocomplete function is supposed to return all words in the Trie that start with a given prefix. What error will it cause when run?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isEndOfWord = 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.isEndOfWord = true;
  }

  autocomplete(prefix) {
    let node = this.root;
    for (const char of prefix) {
      if (!node.children[char]) {
        return [];
      }
      node = node.children[char];
    }
    const results = [];
    function dfs(currentNode, path) {
      if (currentNode.isEndOfWord) {
        results.push(path);
      }
      for (const child in currentNode.children) {
        dfs(currentNode.children[child], path + child);
      }
    }
    dfs(node, prefix);
    return results;
  }
}

const trie = new Trie();
trie.insert('cat');
trie.insert('car');
trie.insert('dog');
console.log(trie.autocomplete('ca'));
ARangeError: Maximum call stack size exceeded
BTypeError: currentNode.children is undefined
CReferenceError: dfs is not defined
DNo error, outputs ['cat', 'car']
Attempts:
2 left
💡 Hint
Check if the dfs function is defined and called correctly.
Predict Output
advanced
2:00remaining
Output of autocomplete with no matching prefix
What is the output of the autocomplete function when searching for prefix 'do' after inserting 'cat', 'car', and 'dog'?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isEndOfWord = 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.isEndOfWord = true;
  }

  autocomplete(prefix) {
    let node = this.root;
    for (const char of prefix) {
      if (!node.children[char]) {
        return [];
      }
      node = node.children[char];
    }
    const results = [];
    function dfs(currentNode, path) {
      if (currentNode.isEndOfWord) {
        results.push(path);
      }
      for (const child in currentNode.children) {
        dfs(currentNode.children[child], path + child);
      }
    }
    dfs(node, prefix);
    return results;
  }
}

const trie = new Trie();
trie.insert('cat');
trie.insert('car');
trie.insert('dog');
console.log(trie.autocomplete('do'));
A['cat', 'car', 'dog']
B['dog']
C[]
Dundefined
Attempts:
2 left
💡 Hint
Check which inserted words start with 'do'.
🧠 Conceptual
expert
2:30remaining
Why use a Trie for Autocomplete?
Which of the following is the best reason to use a Trie data structure for implementing an autocomplete system?
ATries allow fast prefix searches by sharing common prefixes among words, reducing search time compared to scanning all words.
BTries use less memory than hash tables because they store only unique words without duplicates.
CTries automatically sort words alphabetically without extra processing, unlike arrays.
DTries are easier to implement than arrays or hash tables for storing words.
Attempts:
2 left
💡 Hint
Think about how Tries store prefixes and how that helps autocomplete.