0
0
DSA Typescriptprogramming~10 mins

Word Search in Trie in DSA Typescript - Interactive Practice

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

Complete the code to create a Trie node with an empty children map.

DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isEndOfWord: boolean = false;
}

const node = new TrieNode();
node.children.[1]('a', new TrieNode());
Drag options to blanks, or click blank then click option'
Ainsert
Badd
Cpush
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' or 'push' which are not methods of Map.
Trying to assign directly without using a method.
2fill in blank
medium

Complete the code to check if a character exists in the children map of a Trie node.

DSA Typescript
function hasChild(node: TrieNode, char: string): boolean {
  return node.children.[1](char);
}
Drag options to blanks, or click blank then click option'
Ahas
Bcontains
Cincludes
Dexists
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contains' or 'includes' which are not Map methods.
Trying to access children[char] directly without checking.
3fill in blank
hard

Fix the error in the insert method to correctly add characters to the Trie.

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

  insert(word: string): void {
    let current = this.root;
    for (const char of word) {
      if (!current.children.[1](char)) {
        current.children.set(char, new TrieNode());
      }
      current = current.children.get(char)!;
    }
    current.isEndOfWord = true;
  }
}
Drag options to blanks, or click blank then click option'
Aset
Bget
Chas
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' which returns a value, not a boolean.
Using 'set' which adds or updates a value, not checks existence.
4fill in blank
hard

Fill both blanks to complete the search method that returns true if the word exists in the Trie.

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

  search(word: string): boolean {
    let current = this.root;
    for (const char of word) {
      if (!current.children.[1](char)) {
        return false;
      }
      current = current.children.[2](char)!;
    }
    return current.isEndOfWord;
  }
}
Drag options to blanks, or click blank then click option'
Ahas
Bget
Cset
Dcontains
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'get' to retrieve a node.
Using 'contains' which is not a Map method.
5fill in blank
hard

Fill all three blanks to complete the method that checks if any word in the Trie starts with the given prefix.

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

  startsWith(prefix: string): boolean {
    let current = this.root;
    for (const char of prefix) {
      if (!current.children.[1](char)) {
        return false;
      }
      current = current.children.[2](char)!;
    }
    return [3];
  }
}
Drag options to blanks, or click blank then click option'
Ahas
Bget
Ctrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false at the end instead of true.
Using 'set' instead of 'get' to retrieve nodes.