0
0
DSA Typescriptprogramming~10 mins

Why Trie Exists and What Hash Map Cannot Do for Strings in DSA Typescript - Test Your Knowledge

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 node children.

DSA Typescript
if (!node.children.has(word[i])) {
  node.children.set(word[i], new [1]());
}
Drag options to blanks, or click blank then click option'
ATrieNode
BObject
CSet
DArray
Attempts:
3 left
💡 Hint
Common Mistakes
Using Object/Set/Array instead of TrieNode for the child node.
Confusing with the Map structure used for children.
2fill in blank
medium

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

DSA Typescript
for (let i = 0; i < word.length; i++) {
  if (!node.children.has(word[i])) {
    return false;
  }
  node = node.children.get([1])!;
}
return node.isEndOfWord;
Drag options to blanks, or click blank then click option'
Aword
Bword[i]
Ci
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' instead of 'word[i]' which is an index, not a key.
Using 'node' which is the current node, not the child.
3fill in blank
hard

Fix the error in the code to correctly count how many words start with a given prefix.

DSA Typescript
function countPrefix(node: TrieNode, prefix: string): number {
  for (let i = 0; i < prefix.length; i++) {
    if (!node.children.has(prefix[i])) {
      return 0;
    }
    node = node.children.get(prefix[i])!;
  }
  return [1];
}
Drag options to blanks, or click blank then click option'
Anode.count
Bnode.isEndOfWord
Cnode.children.size
Dprefix.length
Attempts:
3 left
💡 Hint
Common Mistakes
Returning isEndOfWord which is a boolean, not a count.
Returning children.size which counts child nodes, not words.
4fill in blank
hard

Fill all three blanks to create a Trie node class with a children map, prefix count, and a boolean flag for word end.

DSA Typescript
class TrieNode {
  children: [1]<string, TrieNode>;
  count: [3];
  isEndOfWord: [2];

  constructor() {
    this.children = new Map();
    this.count = 0;
    this.isEndOfWord = false;
  }
}
Drag options to blanks, or click blank then click option'
AMap
BSet
Cboolean
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using Set instead of Map for children.
Using boolean instead of number for count.
Using number instead of boolean for isEndOfWord.
5fill in blank
hard

Fill all three blanks to implement a function that inserts a word into the Trie and updates counts.

DSA Typescript
function insertWord(root: TrieNode, word: string): void {
  let node = root;
  for (let i = 0; i < word.length; i++) {
    if (!node.children.has(word[i])) {
      node.children.set(word[i], new [1]());
    }
    node = node.children.get(word[i])!;
    node.[2] += 1;
  }
  node.[3] = true;
}
Drag options to blanks, or click blank then click option'
ATrieNode
Bcount
CisEndOfWord
Dchildren
Attempts:
3 left
💡 Hint
Common Mistakes
Using children instead of count for counting prefixes.
Not marking isEndOfWord true at the end.
Wrong type for new node.