0
0
DSA Typescriptprogramming~20 mins

Trie Insert Operation in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trie Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output after inserting words into a Trie
What is the printed structure of the Trie after inserting the words "cat", "car", and "dog" in this order?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isEndOfWord: boolean = false;
}

class Trie {
  root: TrieNode = new TrieNode();

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

  print(node: TrieNode = this.root, prefix: string = '') {
    if (node.isEndOfWord) {
      console.log(prefix);
    }
    for (const [char, child] of node.children) {
      this.print(child, prefix + char);
    }
  }
}

const trie = new Trie();
trie.insert("cat");
trie.insert("car");
trie.insert("dog");
trie.print();
A
"cat"
"car"
"dog"
B
"cat"
"dog"
"car"
C
"dog"
"car"
"cat"
D
"car"
"cat"
"dog"
Attempts:
2 left
💡 Hint
Think about how the children Map stores keys and how iteration order affects output.
🧠 Conceptual
intermediate
1:30remaining
Number of nodes after inserting words in a Trie
After inserting the words "bat", "bath", and "baton" into an empty Trie, how many nodes does the Trie contain?
A9
B7
C8
D10
Attempts:
2 left
💡 Hint
Count shared prefixes carefully to avoid double counting nodes.
🔧 Debug
advanced
2:00remaining
Identify the error in Trie insert method
What error will this Trie insert method cause when inserting the word "apple"?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isEndOfWord: boolean = false;
}

class Trie {
  root: TrieNode = new TrieNode();

  insert(word: string) {
    let node = this.root;
    for (const char of word) {
      if (node.children.has(char)) {
        node.children.set(char, new TrieNode());
      }
      node = node.children.get(char)!;
    }
    node.isEndOfWord = true;
  }
}
ARuntime error: Cannot read property 'get' of undefined
BInfinite loop during insertion
CIncorrect insertion: overwrites existing nodes causing loss of data
DNo error, inserts correctly
Attempts:
2 left
💡 Hint
Check the condition that decides when to create a new node.
📝 Syntax
advanced
1:30remaining
Syntax error in Trie insert method
Which option contains a syntax error in the insert method of a Trie?
DSA Typescript
insert(word: string) {
  let node = this.root;
  for (const char of word) {
    if (!node.children.has(char))
      node.children.set(char, new TrieNode());
    node = node.children.get(char)!;
  }
  node.isEndOfWord = true;
}
AMissing semicolon after set method call causes syntax error
BMissing braces for if statement but valid in TypeScript
CIncorrect use of exclamation mark causes syntax error
DMissing parentheses in for loop causes syntax error
Attempts:
2 left
💡 Hint
Check punctuation after method calls.
🚀 Application
expert
3:00remaining
Final Trie structure after multiple insertions
After inserting the words "to", "tea", "ted", "ten", and "inn" into an empty Trie, which of the following represents the correct set of words printed by a preorder traversal that prints words when isEndOfWord is true?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isEndOfWord: boolean = false;
}

class Trie {
  root: TrieNode = new TrieNode();

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

  print(node: TrieNode = this.root, prefix: string = '') {
    if (node.isEndOfWord) {
      console.log(prefix);
    }
    for (const [char, child] of node.children) {
      this.print(child, prefix + char);
    }
  }
}

const trie = new Trie();
trie.insert("to");
trie.insert("tea");
trie.insert("ted");
trie.insert("ten");
trie.insert("inn");
trie.print();
A
"to"
"tea"
"ted"
"ten"
"inn"
B
"tea"
"ted"
"ten"
"to"
"inn"
C
"to"
"inn"
"tea"
"ted"
"ten"
D
"inn"
"tea"
"ted"
"ten"
"to"
Attempts:
2 left
💡 Hint
Remember that Map iteration preserves insertion order of children.