0
0
DSA Typescriptprogramming~20 mins

Autocomplete System with Trie in DSA Typescript - 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 Trie Insertion and Search
What is the output of the following TypeScript code that inserts words into a Trie and searches for a prefix?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isEndOfWord: boolean = false;
}

class Trie {
  root: TrieNode = new TrieNode();

  insert(word: string): void {
    let node = this.root;
    for (const char of word) {
      if (!node.children.has(char)) {
        node.children.set(char, new TrieNode());
      }
      node = node.children.get(char)!;
    }
    node.isEndOfWord = true;
  }

  searchPrefix(prefix: string): boolean {
    let node = this.root;
    for (const char of prefix) {
      if (!node.children.has(char)) {
        return false;
      }
      node = node.children.get(char)!;
    }
    return true;
  }
}

const trie = new Trie();
trie.insert("apple");
trie.insert("app");
console.log(trie.searchPrefix("app"));
console.log(trie.searchPrefix("appl"));
console.log(trie.searchPrefix("banana"));
A
true
false
false
B
false
false
true
C
false
true
false
D
true
true
false
Attempts:
2 left
💡 Hint
Think about whether the prefix exists in the Trie after insertion.
🧠 Conceptual
intermediate
1:30remaining
Understanding Trie Node Structure
Which of the following best describes the role of the 'isEndOfWord' boolean in a Trie node?
AIt marks the node as the end of a valid word stored in the Trie.
BIt counts how many words pass through this node.
CIt stores the character value of the node.
DIt indicates if the node has any children nodes.
Attempts:
2 left
💡 Hint
Think about how the Trie knows when a word finishes.
🔧 Debug
advanced
2:30remaining
Find the Bug in Autocomplete Suggestion Function
Given the following incomplete autocomplete function in a Trie, which option correctly fixes the bug causing it to return an empty list always?
DSA Typescript
function autocomplete(node: TrieNode, prefix: string): string[] {
  const results: string[] = [];
  function dfs(currentNode: TrieNode, path: string) {
    if (currentNode.isEndOfWord) {
      results.push(path);
    }
    for (const [char, childNode] of currentNode.children) {
      dfs(childNode, path + char);
    }
  }
  dfs(node, prefix);
  return results;
}

// Usage:
// const results = autocomplete(trie.root, "app");
// console.log(results);
AAdd a return statement inside the for loop to stop recursion early
BBefore calling dfs, traverse the Trie to the node matching prefix, then call dfs on that node with empty string
CChange dfs(node, prefix) to dfs(node, "")
DChange dfs(node, prefix) to dfs(node.children.get(prefix[prefix.length - 1])!, prefix)
Attempts:
2 left
💡 Hint
The function must start DFS from the node representing the prefix, not the root.
🚀 Application
advanced
2:00remaining
Predict the Output of Autocomplete Suggestions
After inserting the words ["dog", "deer", "deal"] into a Trie, what will be the output of autocomplete suggestions for prefix "de"?
DSA Typescript
const trie = new Trie();
["dog", "deer", "deal"].forEach(word => trie.insert(word));

function findNode(prefix: string): TrieNode | null {
  let node = trie.root;
  for (const char of prefix) {
    if (!node.children.has(char)) return null;
    node = node.children.get(char)!;
  }
  return node;
}

function autocomplete(node: TrieNode, prefix: string): string[] {
  const results: string[] = [];
  function dfs(currentNode: TrieNode, path: string) {
    if (currentNode.isEndOfWord) results.push(prefix + path);
    for (const [char, childNode] of currentNode.children) {
      dfs(childNode, path + char);
    }
  }
  dfs(node, "");
  return results;
}

const node = findNode("de");
const suggestions = node ? autocomplete(node, "de") : [];
console.log(suggestions);
A["dog", "deer", "deal"]
B["de"]
C["deer", "deal"]
D[]
Attempts:
2 left
💡 Hint
Only words starting with "de" should be suggested.
🧠 Conceptual
expert
2:00remaining
Time Complexity of Autocomplete with Trie
What is the time complexity to find all autocomplete suggestions for a prefix of length p in a Trie containing n words, assuming k suggestions are returned?
AO(p + k * m), where m is average length of suggestions
BO(n * p), because all words must be checked for prefix
CO(k), since only suggestions are returned
DO(p * n), because prefix search multiplies by total words
Attempts:
2 left
💡 Hint
Consider the cost to reach the prefix node and then collect suggestions.