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 TypeScript code that finds the minimum element in a Binary Search Tree (BST)?
DSA Typescript
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } } function findMin(root: Node | null): number | null { if (!root) return null; 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
The function traverses the left children until it reaches the leftmost node, which holds the minimum value 2.
❓ Predict Output
intermediate2:00remaining
Minimum element when BST has only right children
What will the output be when finding the minimum element in a BST where all nodes only have right children?
DSA Typescript
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } } function findMin(root: Node | null): number | null { if (!root) return null; let current = root; while (current.left !== null) { current = current.left; } return current.value; } const root = new Node(3); root.right = new Node(5); root.right.right = new Node(7); root.right.right.right = new Node(10); console.log(findMin(root));
Attempts:
2 left
💡 Hint
If there are no left children, the root is the minimum.
✗ Incorrect
Since there are no left children, the root node value 3 is the minimum.
🔧 Debug
advanced2:00remaining
Identify the error in this BST minimum finder
What error will this TypeScript code produce when trying to find the minimum element in a BST?
DSA Typescript
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } } function findMin(root: Node | null): number | null { if (!root) return null; let current = root; while (current.left) { current = current.right; } return current.value; } const root = new Node(8); root.left = new Node(3); root.left.left = new Node(1); console.log(findMin(root));
Attempts:
2 left
💡 Hint
Check the direction of traversal inside the while loop.
✗ Incorrect
The code incorrectly moves to current.right inside the loop that checks current.left, causing current to become null and then accessing left property causes TypeError.
🧠 Conceptual
advanced2:00remaining
Why does finding minimum in BST take O(h) time?
Why does finding the minimum element in a Binary Search Tree take time proportional to the height (h) of the tree?
Attempts:
2 left
💡 Hint
Think about how you move from root to the smallest value in a BST.
✗ Incorrect
In a BST, the smallest value is the leftmost node. Traversing left children from root to leaf can take up to h steps, where h is the tree height.
❓ Predict Output
expert2:00remaining
Output of findMin on a BST with duplicate values
What is the output of the following code that finds the minimum element in a BST containing duplicate values?
DSA Typescript
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } } function findMin(root: Node | null): number | null { if (!root) return null; let current = root; while (current.left !== null) { current = current.left; } return current.value; } const root = new Node(10); root.left = new Node(10); root.left.left = new Node(5); root.left.left.left = new Node(5); root.right = new Node(15); console.log(findMin(root));
Attempts:
2 left
💡 Hint
Duplicates can appear on the left side; minimum is still the leftmost node.
✗ Incorrect
The leftmost node has value 5, even though duplicates exist, so 5 is the minimum.