0
0
DSA Typescriptprogramming~20 mins

Trie Search Operation in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trie Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Trie Search for Existing Word
What is the output of the following TypeScript code that inserts words into a Trie and searches for the word 'cat'?
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');
trie.insert('dog');
console.log(trie.search('cat'));
ATypeError
Bfalse
Cundefined
Dtrue
Attempts:
2 left
💡 Hint
Think about whether the word 'cat' was inserted before searching.
Predict Output
intermediate
2:00remaining
Output of Trie Search for Non-Existing Word
What is the output of the following TypeScript code that inserts words into a Trie and searches for the word 'bat'?
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');
trie.insert('dog');
console.log(trie.search('bat'));
Aundefined
Btrue
Cfalse
DReferenceError
Attempts:
2 left
💡 Hint
Check if 'bat' was inserted before searching.
🧠 Conceptual
advanced
2:00remaining
Understanding Trie Search Behavior
In a Trie, what does it mean if the search for a word returns false even though all characters of the word are present in the Trie nodes?
AThe word is a prefix but not a complete word stored in the Trie
BThe Trie is empty and has no words stored
CThe search function has a bug and always returns false
DThe word is stored but the search function only checks prefixes
Attempts:
2 left
💡 Hint
Think about the difference between a prefix and a complete word in a Trie.
Predict Output
advanced
2:00remaining
Output of Trie Search After Partial Insertions
What is the output of the following TypeScript code that inserts 'car' and 'cart' into a Trie and searches for 'car' and 'cart'?
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('car');
trie.insert('cart');
console.log(trie.search('car'));
console.log(trie.search('cart'));
Atrue false
Btrue true
Cfalse true
Dfalse false
Attempts:
2 left
💡 Hint
Both 'car' and 'cart' were inserted, so both searches should succeed.
🔧 Debug
expert
2:00remaining
Identify the Bug in Trie Search Implementation
What error will the following TypeScript code produce when searching for the word 'bat' after inserting 'bat' and 'bath' 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('bat');
trie.insert('bath');
console.log(trie.search('bat'));
console.log(trie.search('bath'));
console.log(trie.search('batman'));
Afalse
Btrue
CTypeError
Dundefined
Attempts:
2 left
💡 Hint
Consider what happens when searching for a word that is not inserted but shares a prefix.