0
0
DSA Typescriptprogramming~20 mins

Word Search in Trie in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Trie Search for a Word
What is the output of the following TypeScript code that inserts words into a Trie and searches for a specific word?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isEndOfWord: boolean = false;
}

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

  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.get(char)!;
    }
    node.isEndOfWord = true;
  }

  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.isEndOfWord;
  }
}

const trie = new Trie();
trie.insert("apple");
trie.insert("app");
console.log(trie.search("app"));
ATypeError
Bfalse
Cundefined
Dtrue
Attempts:
2 left
💡 Hint
Think about whether the word 'app' was inserted and if the search checks for complete words.
Predict Output
intermediate
2:00remaining
Trie Search Result for a Non-Inserted Word
What will be the output of the following code when searching for a word not inserted into the Trie?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isEndOfWord: boolean = false;
}

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

  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.get(char)!;
    }
    node.isEndOfWord = true;
  }

  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.isEndOfWord;
  }
}

const trie = new Trie();
trie.insert("cat");
trie.insert("car");
console.log(trie.search("cap"));
Atrue
Bfalse
Cundefined
DReferenceError
Attempts:
2 left
💡 Hint
Check if 'cap' was inserted or if the search path exists in the Trie.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Trie Search Method
What error will the following TypeScript code produce when searching for a word, and why?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isEndOfWord: boolean = false;
}

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

  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.get(char)!;
    }
    node.isEndOfWord = true;
  }

  search(word: string): boolean {
    let node = this.root;
    for (const char of word) {
      node = node.children.get(char)!;
    }
    return node.isEndOfWord;
  }
}

const trie = new Trie();
trie.insert("dog");
console.log(trie.search("don"));
ASyntaxError
Btrue
CTypeError: Cannot read property 'isEndOfWord' of undefined
Dfalse
Attempts:
2 left
💡 Hint
Look at what happens if a character is missing in the children map during search.
🚀 Application
advanced
2:00remaining
Number of Words Stored in Trie
Given the following Trie implementation and inserted words, what is the total number of words stored in the Trie?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isEndOfWord: boolean = false;
}

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

  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.get(char)!;
    }
    node.isEndOfWord = true;
  }

  countWords(node: TrieNode = this.root): number {
    let count = 0;
    if (node.isEndOfWord) count++;
    for (const child of node.children.values()) {
      count += this.countWords(child);
    }
    return count;
  }
}

const trie = new Trie();
trie.insert("bat");
trie.insert("bath");
trie.insert("batman");
trie.insert("bat");
trie.insert("batch");
console.log(trie.countWords());
A4
B5
C3
D6
Attempts:
2 left
💡 Hint
Duplicates do not increase the count. Count only unique complete words.
🧠 Conceptual
expert
2:00remaining
Why Use Trie for Word Search?
Which of the following is the main advantage of using a Trie data structure for word search compared to a simple list of words?
ATries allow searching words in O(m) time where m is the length of the word, independent of the number of words stored.
BTries use less memory than storing words in a list because they compress all words into a single string.
CTries can store infinite words without increasing search time.
DTries automatically sort words alphabetically without extra processing.
Attempts:
2 left
💡 Hint
Think about how search time depends on word length and number of stored words.