0
0
DSA Typescriptprogramming~10 mins

Trie vs Hash Map for Prefix Matching in DSA Typescript - Interactive Comparison Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a word into the Trie.

DSA Typescript
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.[1](char);
  }
  node.isEndOfWord = true;
}
Drag options to blanks, or click blank then click option'
Adelete
Bset
Chas
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'get' to access a child node.
Using 'has' which returns a boolean, not the node.
2fill in blank
medium

Complete the code to check if a prefix exists in the Trie.

DSA Typescript
startsWith(prefix: string): boolean {
  let node = this.root;
  for (const char of prefix) {
    if (!node.children.[1](char)) {
      return false;
    }
    node = node.children.get(char)!;
  }
  return true;
}
Drag options to blanks, or click blank then click option'
Aget
Bset
Chas
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' which returns undefined if key not found, but here we want a boolean check.
Using 'set' which adds or updates a key.
3fill in blank
hard

Fix the error in the Hash Map prefix search function to correctly check prefixes.

DSA Typescript
function hasPrefix(map: Map<string, boolean>, prefix: string): boolean {
  for (let i = 1; i <= prefix.length; i++) {
    const sub = prefix.slice(0, i);
    if (!map.[1](sub)) {
      return false;
    }
  }
  return true;
}
Drag options to blanks, or click blank then click option'
Ahas
Bget
Cset
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' which returns undefined if key not found, but here a boolean check is needed.
Using 'set' which modifies the Map.
4fill in blank
hard

Fill both blanks to create a Trie search method that returns true if a word exists.

DSA Typescript
search(word: string): boolean {
  let node = this.root;
  for (const char of word) {
    if (!node.children.[1](char)) {
      return false;
    }
    node = node.children.[2](char)!;
  }
  return node.isEndOfWord;
}
Drag options to blanks, or click blank then click option'
Ahas
Bget
Cset
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' before checking existence causes errors if key missing.
Using 'set' or 'delete' which modify the Map.
5fill in blank
hard

Fill all three blanks to build a Hash Map prefix insertion that stores all prefixes of a word.

DSA Typescript
function insertPrefixes(map: Map<string, boolean>, word: string): void {
  for (let i = 1; i <= word.length; i++) {
    const prefix = word.[1](0, i);
    map.[2](prefix, true);
  }
  map.[3](word, true);
}
Drag options to blanks, or click blank then click option'
Aslice
Bset
Chas
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' or 'has' instead of 'set' to add keys.
Using wrong string method like 'substr' or 'substring' inconsistently.