Challenge - 5 Problems
BST Minimum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find minimum element in a BST
What is the output of the following code that finds the minimum element in a Binary Search Tree (BST)?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function findMin(root) { let current = root; while (current.left !== null) { current = current.left; } return current.value; } const root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(2); root.left.right = new Node(7); root.right.right = new Node(20); console.log(findMin(root));
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
✗ Incorrect
In a BST, the smallest value is found by going as far left as possible from the root. Here, the leftmost node has value 2.
❓ Predict Output
intermediate2:00remaining
Find minimum in BST with single node
What will the output be when finding the minimum element in a BST with only one node?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function findMin(root) { let current = root; while (current.left !== null) { current = current.left; } return current.value; } const root = new Node(42); console.log(findMin(root));
Attempts:
2 left
💡 Hint
If there is only one node, that node is the minimum.
✗ Incorrect
Since the root has no left child, the loop does not run and the root's value (42) is returned.
❓ Predict Output
advanced2:00remaining
Find minimum element in BST with null root
What error or output will this code produce when trying to find the minimum element in an empty BST (root is null)?
DSA Javascript
function findMin(root) {
let current = root;
while (current.left !== null) {
current = current.left;
}
return current.value;
}
const root = null;
console.log(findMin(root));Attempts:
2 left
💡 Hint
Check what happens when root is null and you try to access root.left.
✗ Incorrect
Since root is null, current is null. Accessing current.left causes a TypeError.
🧠 Conceptual
advanced2:00remaining
Why does findMin in BST only check left children?
Why does the function to find the minimum element in a BST only traverse left children and not right children?
Attempts:
2 left
💡 Hint
Think about the BST property for left and right children.
✗ Incorrect
In a BST, left children have smaller values than their parents, so the smallest value is always found by moving left until no more left child exists.
❓ Predict Output
expert2:00remaining
Find minimum element after BST modification
Given the BST below, what is the output after inserting a new node with value 1 and then finding the minimum element?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function insert(root, value) { if (root === null) return new Node(value); if (value < root.value) { root.left = insert(root.left, value); } else { root.right = insert(root.right, value); } return root; } function findMin(root) { let current = root; while (current.left !== null) { current = current.left; } return current.value; } const root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(2); root.left.right = new Node(7); root.right.right = new Node(20); insert(root, 1); console.log(findMin(root));
Attempts:
2 left
💡 Hint
After inserting 1, it becomes the new leftmost node.
✗ Incorrect
The new node with value 1 is inserted as the left child of 2, making 1 the smallest value in the BST.