0
0
DSA Javascriptprogramming~20 mins

Trie Search Operation in DSA Javascript - 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
What is the output of the Trie search for 'cat'?
Given the following Trie implementation and insertions, what will be the output of searching for the word 'cat'?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isEndOfWord = false;
  }
}

class Trie {
  constructor() {
    this.root = new TrieNode();
  }

  insert(word) {
    let node = this.root;
    for (const char of word) {
      if (!node.children[char]) {
        node.children[char] = new TrieNode();
      }
      node = node.children[char];
    }
    node.isEndOfWord = true;
  }

  search(word) {
    let node = this.root;
    for (const char of word) {
      if (!node.children[char]) {
        return false;
      }
      node = node.children[char];
    }
    return node.isEndOfWord;
  }
}

const trie = new Trie();
trie.insert('cat');
trie.insert('car');
trie.insert('dog');

console.log(trie.search('cat'));
Atrue
BTypeError
Cundefined
Dfalse
Attempts:
2 left
💡 Hint
Check if the search method returns true only when the word is fully present and marked as end of word.
Predict Output
intermediate
2:00remaining
What is the output of searching for 'ca' in the Trie?
Using the same Trie from the previous example, what will be the output of searching for the word 'ca'?
DSA Javascript
console.log(trie.search('ca'));
Atrue
Bfalse
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
Remember that 'ca' is a prefix but not a complete word inserted.
🧠 Conceptual
advanced
2:00remaining
Which statement about Trie search operation is correct?
Choose the correct statement about how the search operation works in a Trie data structure.
ASearch returns true if all characters of the word are found in sequence and the last node is marked as end of word.
BSearch returns true if the word is a prefix of any inserted word, regardless of end of word marking.
CSearch returns true if the first character of the word is found in the Trie root children.
DSearch returns true only if the word length is greater than 5 characters.
Attempts:
2 left
💡 Hint
Think about what makes a word complete in a Trie.
🔧 Debug
advanced
2:00remaining
What error occurs when searching for a word with a missing character node?
Consider the Trie search method. What happens if the search word contains a character not present in the Trie nodes?
DSA Javascript
search(word) {
  let node = this.root;
  for (const char of word) {
    if (!node.children[char]) {
      return false;
    }
    node = node.children[char];
  }
  return node.isEndOfWord;
}
AReturns undefined
BThrows a ReferenceError
CReturns false indicating word not found
DThrows a TypeError
Attempts:
2 left
💡 Hint
Check the condition when a character node is missing.
🚀 Application
expert
3:00remaining
How many words are found after inserting and searching in the Trie?
After inserting the words ['apple', 'app', 'application', 'bat', 'bath'] into an empty Trie, how many of these words will return true when searched?
DSA Javascript
const trie = new Trie();
['apple', 'app', 'application', 'bat', 'bath'].forEach(word => trie.insert(word));

const results = ['apple', 'app', 'application', 'bat', 'bath'].map(word => trie.search(word));
const count = results.filter(Boolean).length;
console.log(count);
A4
B2
C3
D5
Attempts:
2 left
💡 Hint
All inserted words are marked as end of word nodes.