Challenge - 5 Problems
BST Maximum Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find the maximum element in a BST
What is the output of the following TypeScript code that finds the maximum 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 findMax(root: Node | null): number | null { if (!root) return null; let current = root; while (current.right !== null) { current = current.right; } return current.value; } const root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.right.right = new Node(20); root.right.left = new Node(12); console.log(findMax(root));
Attempts:
2 left
💡 Hint
The maximum element in a BST is the rightmost node.
✗ Incorrect
In a BST, the maximum value is found by going as far right as possible. Here, the rightmost node has value 20.
❓ Predict Output
intermediate2:00remaining
Output of findMax on a single-node BST
What will be printed when findMax is called on a BST with only one node?
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 findMax(root: Node | null): number | null { if (!root) return null; let current = root; while (current.right !== null) { current = current.right; } return current.value; } const root = new Node(42); console.log(findMax(root));
Attempts:
2 left
💡 Hint
If there is only one node, it is both minimum and maximum.
✗ Incorrect
Since the tree has only one node with value 42, that is the maximum.
🔧 Debug
advanced2:00remaining
Identify the error in this BST maximum finder
What error will this TypeScript code produce when trying to find the maximum 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 findMax(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.right.right = new Node(20); root.right.left = new Node(12); console.log(findMax(root));
Attempts:
2 left
💡 Hint
Check which child pointer is used to traverse to find maximum.
✗ Incorrect
The code incorrectly traverses left children, which leads to the minimum value, not maximum.
❓ Predict Output
advanced2:00remaining
Output when BST is empty
What is the output of the following code when the BST is empty (null root)?
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 findMax(root: Node | null): number | null { if (!root) return null; let current = root; while (current.right !== null) { current = current.right; } return current.value; } const root = null; console.log(findMax(root));
Attempts:
2 left
💡 Hint
Check the base case for an empty tree.
✗ Incorrect
The function returns null immediately if the root is null, indicating no maximum.
🧠 Conceptual
expert2:00remaining
Why does traversing right child nodes find the maximum in a BST?
In a Binary Search Tree (BST), why does moving to the right child repeatedly lead to the maximum element?
Attempts:
2 left
💡 Hint
Recall the BST property about left and right children values.
✗ Incorrect
By definition, BST nodes have all right children greater than the node itself, so moving right leads to the largest value.