Challenge - 5 Problems
BST Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of the BST after these insertions?
Consider a Binary Search Tree (BST) initially empty. Insert the values 10, 5, 15, 3, 7 in this order. What is the in-order traversal output of the BST?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } class BST { constructor() { this.root = null; } insert(value) { const newNode = new Node(value); if (!this.root) { this.root = newNode; return; } let current = this.root; while (true) { if (value < current.value) { if (!current.left) { current.left = newNode; return; } current = current.left; } else { if (!current.right) { current.right = newNode; return; } current = current.right; } } } inorder(node, result = []) { if (!node) return result; this.inorder(node.left, result); result.push(node.value); this.inorder(node.right, result); return result; } } const tree = new BST(); tree.insert(10); tree.insert(5); tree.insert(15); tree.insert(3); tree.insert(7); console.log(tree.inorder(tree.root));
Attempts:
2 left
💡 Hint
In-order traversal of a BST returns values in sorted order.
✗ Incorrect
Inserting values 10, 5, 15, 3, 7 creates a BST where in-order traversal visits nodes in ascending order: 3, 5, 7, 10, 15.
❓ Predict Output
intermediate2:00remaining
What is the BST structure after inserting 8 into this tree?
Given a BST with nodes 10 (root), 5 (left child), and 15 (right child), what is the in-order traversal after inserting 8?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } class BST { constructor() { this.root = null; } insert(value) { const newNode = new Node(value); if (!this.root) { this.root = newNode; return; } let current = this.root; while (true) { if (value < current.value) { if (!current.left) { current.left = newNode; return; } current = current.left; } else { if (!current.right) { current.right = newNode; return; } current = current.right; } } } inorder(node, result = []) { if (!node) return result; this.inorder(node.left, result); result.push(node.value); this.inorder(node.right, result); return result; } } const tree = new BST(); tree.root = new Node(10); tree.root.left = new Node(5); tree.root.right = new Node(15); tree.insert(8); console.log(tree.inorder(tree.root));
Attempts:
2 left
💡 Hint
Insert 8 as right child of 5 because 8 > 5 and 8 < 10.
✗ Incorrect
8 is inserted as the right child of 5. In-order traversal visits nodes in ascending order: 5, 8, 10, 15.
🔧 Debug
advanced2:00remaining
Why does this BST insert code cause an infinite loop?
Look at this insert method for BST. Why does it cause an infinite loop when inserting a value equal to a node's value?
DSA Javascript
insert(value) {
const newNode = new Node(value);
if (!this.root) {
this.root = newNode;
return;
}
let current = this.root;
while (true) {
if (value < current.value) {
if (!current.left) {
current.left = newNode;
return;
}
current = current.left;
} else if (value > current.value) {
if (!current.right) {
current.right = newNode;
return;
}
current = current.right;
}
}
}Attempts:
2 left
💡 Hint
Check what happens when value equals current.value.
✗ Incorrect
When value equals current.value, neither if nor else if conditions are true, so current never changes and loop runs forever.
❓ Predict Output
advanced2:00remaining
What is the output after inserting duplicate values in BST?
Given a BST where duplicates are inserted to the right subtree, what is the in-order traversal after inserting 10, 5, 15, 10, 5?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } class BST { constructor() { this.root = null; } insert(value) { const newNode = new Node(value); if (!this.root) { this.root = newNode; return; } let current = this.root; while (true) { if (value < current.value) { if (!current.left) { current.left = newNode; return; } current = current.left; } else { if (!current.right) { current.right = newNode; return; } current = current.right; } } } inorder(node, result = []) { if (!node) return result; this.inorder(node.left, result); result.push(node.value); this.inorder(node.right, result); return result; } } const tree = new BST(); tree.insert(10); tree.insert(5); tree.insert(15); tree.insert(10); tree.insert(5); console.log(tree.inorder(tree.root));
Attempts:
2 left
💡 Hint
Duplicates go to the right subtree in this BST.
✗ Incorrect
Duplicates 10 and 5 are inserted as right children of existing nodes with same values, so in-order traversal shows duplicates in sorted order.
🧠 Conceptual
expert2:00remaining
What is the time complexity of inserting N sorted values into an empty BST?
If you insert N values sorted in ascending order into an empty BST, what is the time complexity of all insertions combined?
Attempts:
2 left
💡 Hint
Think about how the tree shape changes when inserting sorted values.
✗ Incorrect
Inserting sorted values creates a skewed tree (like a linked list), so each insertion takes O(N) in worst case, total O(N^2).