0
0
DSA Typescriptprogramming~20 mins

Trie vs Hash Map for Prefix Matching in DSA Typescript - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prefix Matching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Prefix Search Using Trie
What is the output of the following TypeScript code that uses a Trie to find words with a given prefix?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isWord: boolean = false;
}

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

  insert(word: string) {
    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.isWord = 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 = (currNode: TrieNode, path: string) => {
      if (currNode.isWord) results.push(path);
      for (const [ch, nextNode] of currNode.children) {
        dfs(nextNode, path + ch);
      }
    };
    dfs(node, prefix);
    return results;
  }
}

const trie = new Trie();
['apple', 'app', 'apex', 'bat', 'bath'].forEach(w => trie.insert(w));
console.log(trie.searchPrefix('ap'));
A["apple", "apex"]
B["app", "apple", "apex"]
C["app", "apex"]
D["bat", "bath"]
Attempts:
2 left
💡 Hint
Think about all words starting with 'ap' inserted into the trie.
Predict Output
intermediate
2:00remaining
Output of Prefix Search Using Hash Map
What is the output of the following TypeScript code that uses a Hash Map to find words with a given prefix?
DSA Typescript
const words = ['app', 'apple', 'apex', 'bat', 'bath'];
const prefix = 'ap';
const result = words.filter(word => word.startsWith(prefix));
console.log(result);
A["bat", "bath"]
B["apple", "apex"]
C["app", "apex"]
D["app", "apple", "apex"]
Attempts:
2 left
💡 Hint
Filter words that start with the prefix 'ap'.
🧠 Conceptual
advanced
2:00remaining
Why Use Trie Over Hash Map for Prefix Matching?
Which of the following is the main advantage of using a Trie over a Hash Map for prefix matching in large datasets?
ATrie allows efficient prefix search with time complexity proportional to prefix length, not dataset size.
BTrie uses less memory than a Hash Map for storing all words.
CHash Map cannot store strings as keys.
DTrie automatically sorts words alphabetically without extra steps.
Attempts:
2 left
💡 Hint
Consider how search time depends on prefix length vs number of words.
🧠 Conceptual
advanced
2:00remaining
Memory Usage Comparison Between Trie and Hash Map
Which statement best describes memory usage when comparing Trie and Hash Map for storing many words?
AHash Map uses more memory because it stores characters individually.
BHash Map always uses less memory because it stores only full words as keys.
CTrie can use more memory due to storing nodes for each character, especially with sparse data.
DTrie uses less memory because it compresses common prefixes automatically.
Attempts:
2 left
💡 Hint
Think about how tries store each character as a node.
🔧 Debug
expert
3:00remaining
Identify the Bug in Trie Prefix Search Implementation
Given the following TypeScript Trie prefix search code, what error will it produce when searching for prefix 'ap'?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isWord: boolean = false;
}

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

  insert(word: string) {
    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.isWord = 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 = (currNode: TrieNode, path: string) => {
      if (currNode.isWord) results.push(path);
      for (const [ch, nextNode] of currNode.children) {
        dfs(nextNode, path + ch);
      }
    };
    dfs(node, prefix);
    return results;
  }
}

const trie = new Trie();
['apple', 'app', 'apex', 'bat', 'bath'].forEach(w => trie.insert(w));
console.log(trie.searchPrefix('ap'));
ANo error; outputs ["app", "apple", "apex"]
BTypeError because node.children is undefined
CStack overflow due to infinite recursion in dfs
DSyntaxError due to missing return statement in dfs
Attempts:
2 left
💡 Hint
Check how dfs is called and if recursion ends properly.