0
0
DSA Typescriptprogramming~20 mins

Trie Node Design and Initialization in DSA Typescript - 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 TypeScript code that initializes a Trie node and prints its children keys?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode>;
  isEndOfWord: boolean;

  constructor() {
    this.children = new Map();
    this.isEndOfWord = false;
  }
}

const node = new TrieNode();
console.log(Array.from(node.children.keys()));
A[null]
B[undefined]
C[]
D[" "]
Attempts:
2 left
💡 Hint
Think about what keys exist in a newly created empty Map.
Predict Output
intermediate
2:00remaining
Trie Node Children After Insert
After inserting a child node with key 'a' into a TrieNode's children Map, what is the output of printing the keys?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode>;
  isEndOfWord: boolean;

  constructor() {
    this.children = new Map();
    this.isEndOfWord = false;
  }
}

const node = new TrieNode();
node.children.set('a', new TrieNode());
console.log(Array.from(node.children.keys()));
A[undefined]
B['a']
C['A']
D[]
Attempts:
2 left
💡 Hint
Check what key was added to the Map.
🔧 Debug
advanced
2:00remaining
Identify the Error in TrieNode Constructor
What error will this TypeScript code produce when creating a TrieNode instance?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode>;
  isEndOfWord: boolean;

  constructor() {
    this.children = {};
    this.children.set('a', new TrieNode());
    this.isEndOfWord = false;
  }
}

const node = new TrieNode();
ATypeError: this.children.set is not a function
BSyntaxError: Unexpected token {
CNo error, code runs fine
DReferenceError: TrieNode is not defined
Attempts:
2 left
💡 Hint
Check the type assigned to children and how Map methods are used.
🧠 Conceptual
advanced
1:30remaining
Purpose of isEndOfWord in TrieNode
What is the main purpose of the isEndOfWord boolean property in a TrieNode?
ATo mark if the node represents the end of a valid word
BTo store the number of children nodes
CTo keep track of the depth of the node in the Trie
DTo hold the character value of the node
Attempts:
2 left
💡 Hint
Think about how a Trie distinguishes complete words from prefixes.
Predict Output
expert
3:00remaining
Final Trie Structure After Multiple Insertions
Given the following code inserting words into a Trie, what is the output of printing the root node's children keys?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode>;
  isEndOfWord: boolean;

  constructor() {
    this.children = new Map();
    this.isEndOfWord = false;
  }
}

class Trie {
  root: TrieNode;

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

  insert(word: string) {
    let current = this.root;
    for (const char of word) {
      if (!current.children.has(char)) {
        current.children.set(char, new TrieNode());
      }
      current = current.children.get(char)!;
    }
    current.isEndOfWord = true;
  }
}

const trie = new Trie();
trie.insert('cat');
trie.insert('car');
trie.insert('dog');
console.log(Array.from(trie.root.children.keys()));
A['cat', 'car', 'dog']
B['a', 'o']
C[]
D['c', 'd']
Attempts:
2 left
💡 Hint
The root's children keys are the first letters of inserted words.