0
0
DSA Javascriptprogramming~20 mins

Word Search in Trie in DSA Javascript - 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 Searching a Word in Trie
What is the output of the following code that inserts words into a Trie and searches for a specific word?
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('car'));
Atrue
Bundefined
Cfalse
DTypeError
Attempts:
2 left
💡 Hint
Check if the word 'car' was inserted and if the search method returns true only for complete words.
🧠 Conceptual
intermediate
1:30remaining
Understanding Trie Node Structure
Which of the following best describes the role of the 'isEndOfWord' property in a Trie node?
AIt counts how many words pass through this node.
BIt marks the node as the end of a valid word stored in the Trie.
CIt stores the character value of the node.
DIt points to the parent node in the Trie.
Attempts:
2 left
💡 Hint
Think about how the Trie knows when a word ends.
🔧 Debug
advanced
2:00remaining
Identify the Error in Trie Search Method
What error will occur when running this code snippet that searches for a word in a Trie?
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('apple');
console.log(trie.search('app'));
ATypeError
Btrue
Cfalse
Dundefined
Attempts:
2 left
💡 Hint
Check if 'app' was inserted as a complete word or just a prefix.
🚀 Application
advanced
2:00remaining
Result of Inserting and Searching Multiple Words
After inserting the words ['bat', 'bath', 'batman', 'batmobile'] into an empty Trie, what will be the output of searching for 'batm'?
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();
['bat', 'bath', 'batman', 'batmobile'].forEach(word => trie.insert(word));
console.log(trie.search('batm'));
Afalse
BTypeError
Cundefined
Dtrue
Attempts:
2 left
💡 Hint
Check if 'batm' was inserted as a complete word or just a prefix.
🧠 Conceptual
expert
2:30remaining
Trie Memory Usage and Optimization
Which of the following statements about Trie memory usage and optimization is correct?
AUsing arrays instead of objects for children always reduces Trie memory usage.
BTries always use less memory than hash tables because they share prefixes.
CTries cannot be optimized for memory and always use large amounts of space.
DCompressed Tries reduce memory by merging nodes with a single child into one node.
Attempts:
2 left
💡 Hint
Think about how nodes with only one child can be merged to save space.