0
0
DSA Javascriptprogramming~20 mins

Trie Insert Operation 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
What is the output after inserting words into a Trie?
Consider a Trie data structure where each node stores children in an object and a boolean flag for end of word. After inserting the words "cat" and "car", what is the printed structure of the root node's children keys?
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;
  }
}

const trie = new Trie();
trie.insert("cat");
trie.insert("car");
console.log(Object.keys(trie.root.children));
A["c", "a", "t", "r"]
B["c", "a"]
C["c"]
D["a"]
Attempts:
2 left
💡 Hint
Think about the first letter of each inserted word and how the root node stores children.
Predict Output
intermediate
2:00remaining
What is the value of isEndOfWord after inserting 'dog' and 'dot'?
After inserting the words "dog" and "dot" into an empty Trie, what is the value of isEndOfWord for the node representing the letter 'g' in "dog"?
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;
  }
}

const trie = new Trie();
trie.insert("dog");
trie.insert("dot");
console.log(trie.root.children['d'].children['o'].children['g'].isEndOfWord);
Aundefined
Btrue
Cfalse
DTypeError
Attempts:
2 left
💡 Hint
Check if the last character node of the inserted word marks the end of a word.
Predict Output
advanced
2:00remaining
What is the output after inserting overlapping words in a Trie?
Given the following insertions into a Trie: "bat", "bath", and "baton", what is the value of isEndOfWord for the node representing 't' in "bat"?
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;
  }
}

const trie = new Trie();
trie.insert("bat");
trie.insert("bath");
trie.insert("baton");
console.log(trie.root.children['b'].children['a'].children['t'].isEndOfWord);
Atrue
Bfalse
Cundefined
DReferenceError
Attempts:
2 left
💡 Hint
Remember that each inserted word marks the end of its last character node.
🧠 Conceptual
advanced
2:00remaining
How many nodes are created after inserting 'apple' and 'app' into an empty Trie?
If you insert the words "apple" and "app" into an empty Trie, how many total nodes (including the root) will the Trie have?
A7
B5
C8
D6
Attempts:
2 left
💡 Hint
Count nodes for unique characters only; shared prefixes reuse nodes.
Predict Output
expert
2:00remaining
What is the output after inserting words and checking a non-existent path?
After inserting "hello" and "helium" into a Trie, what is the output of accessing trie.root.children['h'].children['e'].children['l'].children['x']?
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;
  }
}

const trie = new Trie();
trie.insert("hello");
trie.insert("helium");
console.log(trie.root.children['h'].children['e'].children['l'].children['x']);
Aundefined
Bnull
C{}
DTypeError
Attempts:
2 left
💡 Hint
Check if the path for 'x' exists under the given nodes.