0
0
DSA Typescriptprogramming~20 mins

Prefix Search Using Trie in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prefix Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Prefix Search Result
What is the output of the following TypeScript code that inserts words into a Trie and searches for words starting with 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): string[] {
    let node = this.root;
    for (const char of prefix) {
      if (!node.children.has(char)) {
        return [];
      }
      node = node.children.get(char)!;
    }
    const results: string[] = [];
    const 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;
  }
}

const trie = new Trie();
trie.insert("apple");
trie.insert("app");
trie.insert("apex");
trie.insert("bat");
trie.insert("bath");
const result = trie.searchPrefix("ap");
console.log(result);
A["app", "apple", "apex"]
B["apple", "app", "apex"]
C["apex", "app", "apple"]
D["app", "apex"]
Attempts:
2 left
💡 Hint
Think about how the DFS collects words in alphabetical order based on children map iteration.
🧠 Conceptual
intermediate
1:00remaining
Number of Words with Given Prefix
If a Trie contains the words ["cat", "car", "cart", "dog", "dove"], how many words start with the prefix "car"?
A0
B2
C3
D1
Attempts:
2 left
💡 Hint
Count all words that begin exactly with 'car'.
🔧 Debug
advanced
1:30remaining
Identify the Bug in Trie Insertion
What error will occur when running this TypeScript code snippet for inserting a word into a Trie?
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); // Bug here
    }
    node.isEndOfWord = true;
  }
}

const trie = new Trie();
trie.insert("hello");
ANo error, runs correctly
BSyntaxError: Unexpected token
CReferenceError: node is not defined
DTypeError: Cannot read property 'isEndOfWord' of undefined
Attempts:
2 left
💡 Hint
Check the return type of Map.get and how it's used.
📝 Syntax
advanced
1:30remaining
Correct Syntax for DFS in Trie Search
Which option correctly defines the DFS function inside the searchPrefix method to collect all words starting with a prefix?
DSA Typescript
dfs = (currentNode: TrieNode, path: string) => {
  if (currentNode.isEndOfWord) {
    results.push(path);
  }
  for (const [char, childNode] of currentNode.children) {
    dfs(childNode, path + char);
  }
};
A
function dfs(currentNode: TrieNode, path: string) {
  if (currentNode.isEndOfWord) {
    results.push(path);
  }
  for (const [char, childNode] of currentNode.children) {
    dfs(childNode, path + char);
  }
}
B
dfs(currentNode: TrieNode, path: string) =&gt; {
  if (currentNode.isEndOfWord) {
    results.push(path);
  }
  for (const [char, childNode] of currentNode.children) {
    dfs(childNode, path + char);
  }
};
C
const dfs = (currentNode: TrieNode, path: string) =&gt; {
  if (currentNode.isEndOfWord) {
    results.push(path);
  }
  for (const [char, childNode] of currentNode.children) {
    dfs(childNode, path + char);
  }
};
D
const dfs = function(currentNode: TrieNode, path: string) {
  if (currentNode.isEndOfWord) {
    results.push(path);
  }
  for (const [char, childNode] of currentNode.children) {
    dfs(childNode, path + char);
  }
};
Attempts:
2 left
💡 Hint
Arrow functions with const are common for nested functions in TypeScript.
🚀 Application
expert
2:30remaining
Find All Words with Prefix and Sort Alphabetically
Given a Trie with words inserted in any order, which code modification ensures that searchPrefix returns all words starting with the prefix sorted alphabetically?
DSA Typescript
searchPrefix(prefix: string): string[] {
  let node = this.root;
  for (const char of prefix) {
    if (!node.children.has(char)) {
      return [];
    }
    node = node.children.get(char)!;
  }
  const results: string[] = [];
  const 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;
}
AModify DFS to visit children in alphabetical order by sorting keys before recursion.
BChange Map to a JavaScript object and iterate keys sorted alphabetically.
CUse a PriorityQueue to store words during DFS and dequeue all at the end.
DSort the results array before returning: return results.sort();
Attempts:
2 left
💡 Hint
Think about controlling the order of traversal rather than sorting after collection.