0
0
DSA Typescriptprogramming~10 mins

Longest Word in Dictionary Using 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 insert a word into the Trie.

DSA Typescript
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[1];
  }
  node.isEnd = true;
}
Drag options to blanks, or click blank then click option'
A.children.has(char)
B.children.set(char)
C.children.get(char)
D.isEnd
Attempts:
3 left
💡 Hint
Common Mistakes
Using .children.set(char) instead of .children.get(char) to move to the child node.
Trying to access node.isEnd instead of child nodes.
2fill in blank
medium

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

DSA Typescript
search(word: string): boolean {
  let node = this.root;
  for (const char of word) {
    if (!node.children.has(char)) {
      return false;
    }
    node = node.children.get(char)!;
  }
  return node[1];
}
Drag options to blanks, or click blank then click option'
A.isEnd
B.children
C.has
D.get
Attempts:
3 left
💡 Hint
Common Mistakes
Returning node.children instead of node.isEnd.
Using node.has or node.get which are Map methods, not properties of TrieNode.
3fill in blank
hard

Fix the error in the DFS function to find the longest word built one character at a time.

DSA Typescript
dfs(node: TrieNode, path: string): string {
  let longest = path;
  for (const [char, child] of node.children) {
    if (child[1]) {
      const word = this.dfs(child, path + char);
      if (word.length > longest.length || (word.length === longest.length && word < longest)) {
        longest = word;
      }
    }
  }
  return longest;
}
Drag options to blanks, or click blank then click option'
A.children
B.isEnd
C.has
D.get
Attempts:
3 left
💡 Hint
Common Mistakes
Using child.children or child.has which are incorrect here.
Not checking if child.isEnd, causing invalid words to be included.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters words with length greater than 3 and maps them to their lengths.

DSA Typescript
const filteredLengths = {word[1] word of words if word.length [2] 3};
Drag options to blanks, or click blank then click option'
A: word.length
B>
C<
D=>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for filtering.
Using '=>' which is for arrow functions, not object literals.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 2.

DSA Typescript
const result = { [1]: [2] for const word of words if word.length [3] 2 };
Drag options to blanks, or click blank then click option'
Aword.toUpperCase()
Bword.length
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of 'word.toUpperCase()' for keys.
Using '<' instead of '>' for filtering.