Challenge - 5 Problems
BST Maximum Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind maximum element in a BST - output after insertion
Given the following Java code that inserts elements into a Binary Search Tree (BST) and then finds the maximum element, what will be the printed output?
DSA Java
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } class BST { Node root; void insert(int data) { root = insertRec(root, data); } Node insertRec(Node root, int data) { if (root == null) { root = new Node(data); return root; } if (data < root.data) { root.left = insertRec(root.left, data); } else if (data > root.data) { root.right = insertRec(root.right, data); } return root; } int findMax() { Node current = root; while (current.right != null) { current = current.right; } return current.data; } } public class Main { public static void main(String[] args) { BST tree = new BST(); tree.insert(15); tree.insert(10); tree.insert(20); tree.insert(8); tree.insert(12); tree.insert(25); System.out.println(tree.findMax()); } }
Attempts:
2 left
💡 Hint
The maximum element in a BST is the rightmost node.
✗ Incorrect
In a BST, the maximum element is found by going to the right child repeatedly until no more right child exists. Here, 25 is the rightmost node.
❓ Predict Output
intermediateWhat is the output when the BST is empty?
Consider the same BST class as before. What happens when findMax() is called on an empty BST (no nodes inserted)?
DSA Java
class BST { Node root; int findMax() { Node current = root; while (current.right != null) { current = current.right; } return current.data; } } public class Main { public static void main(String[] args) { BST tree = new BST(); System.out.println(tree.findMax()); } }
Attempts:
2 left
💡 Hint
What happens if you try to access a property of a null object?
✗ Incorrect
Since root is null, current is null. Accessing current.right causes NullPointerException.
🧠 Conceptual
advancedWhy does findMax() only traverse right children in a BST?
In a Binary Search Tree, why is it sufficient to traverse only the right children to find the maximum element?
Attempts:
2 left
💡 Hint
Recall the BST property about left and right children values.
✗ Incorrect
In BST, left child nodes have smaller values and right child nodes have greater values than their parent. So, the maximum is the rightmost node.
🔧 Debug
advancedIdentify the bug in this findMax() method
What is wrong with the following findMax() method in a BST class?
DSA Java
int findMax() { Node current = root; while (current.left != null) { current = current.left; } return current.data; }
Attempts:
2 left
💡 Hint
Think about which side holds larger values in a BST.
✗ Incorrect
Traversing left children finds the minimum, not maximum. Also, if root is null, NullPointerException occurs. So all options apply.
🚀 Application
expertAfter inserting nodes, what is the maximum element in this BST?
You insert the following integers into an empty BST in this order: 30, 20, 40, 35, 50, 45. What is the maximum element in the BST after all insertions?
Attempts:
2 left
💡 Hint
The maximum is the rightmost node after all insertions.
✗ Incorrect
The largest value inserted is 50, which will be the rightmost node in the BST.
