0
0
DSA Javascriptprogramming~20 mins

Trie Node Design and Initialization 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 Trie Node Initialization
What is the output of the following JavaScript code that initializes a Trie node and prints its properties?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isEndOfWord = false;
  }
}

const node = new TrieNode();
console.log(JSON.stringify(node));
A{"children":null,"isEndOfWord":false}
B{"children":{},"isEndOfWord":true}
C{"children":[],"isEndOfWord":false}
D{"children":{},"isEndOfWord":false}
Attempts:
2 left
💡 Hint
Look at how the children property is initialized and the default value of isEndOfWord.
Predict Output
intermediate
2:00remaining
Trie Node Children Property Type
What will be the output of this code snippet that checks the type of the children property in a Trie node?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isEndOfWord = false;
  }
}

const node = new TrieNode();
console.log(typeof node.children);
A"null"
B"object"
C"array"
D"undefined"
Attempts:
2 left
💡 Hint
Remember what type JavaScript returns for objects and arrays.
Predict Output
advanced
2:00remaining
Adding a Child Node to Trie Node
What is the output after adding a child node with key 'a' to the children of a Trie node and printing the children keys?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isEndOfWord = false;
  }
}

const node = new TrieNode();
node.children['a'] = new TrieNode();
console.log(Object.keys(node.children));
A["children"]
B[]
C["a"]
D["isEndOfWord"]
Attempts:
2 left
💡 Hint
Check what keys are present in the children object after adding a new child.
🧠 Conceptual
advanced
1:30remaining
Purpose of isEndOfWord in Trie Node
What is the main purpose of the isEndOfWord property in a Trie node?
ATo mark the end of a complete word in the Trie
BTo store the number of children nodes
CTo hold the character value of the node
DTo indicate if the node has any children
Attempts:
2 left
💡 Hint
Think about how a Trie distinguishes between prefixes and complete words.
Predict Output
expert
2:30remaining
Output After Multiple Insertions in Trie Node
Given the following code that inserts characters into a Trie node's children, what is the output of printing the children keys of the root node?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isEndOfWord = false;
  }
}

const root = new TrieNode();
const word = 'cat';
for (const char of word) {
  if (!root.children[char]) {
    root.children[char] = new TrieNode();
  }
}
console.log(Object.keys(root.children));
A["c", "a", "t"]
B[]
C["cat"]
D["t"]
Attempts:
2 left
💡 Hint
Each character is added as a separate child key if it does not exist.