Challenge - 5 Problems
BST Max Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the maximum element in a BST
What is the output of the following C# code that finds the maximum element in a Binary Search Tree (BST)?
DSA C#
using System; class Node { public int data; public Node left, right; public Node(int item) { data = item; left = right = null; } } class BST { public Node root; public int FindMax(Node node) { if (node == null) return int.MinValue; while (node.right != null) { node = node.right; } return node.data; } } class Program { static void Main() { BST tree = new BST(); tree.root = new Node(10); tree.root.left = new Node(5); tree.root.right = new Node(20); tree.root.right.left = new Node(15); tree.root.right.right = new Node(25); Console.WriteLine(tree.FindMax(tree.root)); } }
Attempts:
2 left
💡 Hint
In a BST, the maximum element is the rightmost node.
✗ Incorrect
The code traverses the right children until it reaches the rightmost node, which holds the maximum value. Here, 25 is the rightmost node.
❓ Predict Output
intermediateOutput when BST is empty
What will be the output of the following code when the BST is empty (root is null)?
DSA C#
using System; class Node { public int data; public Node left, right; public Node(int item) { data = item; left = right = null; } } class BST { public Node root; public int FindMax(Node node) { if (node == null) return int.MinValue; while (node.right != null) { node = node.right; } return node.data; } } class Program { static void Main() { BST tree = new BST(); Console.WriteLine(tree.FindMax(tree.root)); } }
Attempts:
2 left
💡 Hint
Check the return value when the input node is null.
✗ Incorrect
The method returns int.MinValue when the root is null, which is -2147483648 in C#.
🔧 Debug
advancedIdentify the error in FindMax method
What error will the following code produce when trying to find the maximum element in a BST?
DSA C#
public int FindMax(Node node) { while (node.right != null) { node = node.left; } return node.data; }
Attempts:
2 left
💡 Hint
Check which child node is used inside the loop.
✗ Incorrect
The code incorrectly moves to the left child inside the loop instead of the right child, so it returns the minimum element, not the maximum.
❓ Predict Output
advancedMaximum element after inserting nodes
After inserting the following nodes into an empty BST in this order: 30, 10, 40, 35, 50, what is the maximum element found by FindMax?
DSA C#
using System; class Node { public int data; public Node left, right; public Node(int item) { data = item; left = right = null; } } class BST { public Node root; public void Insert(int key) { root = InsertRec(root, key); } private Node InsertRec(Node root, int key) { if (root == null) { root = new Node(key); return root; } if (key < root.data) root.left = InsertRec(root.left, key); else if (key > root.data) root.right = InsertRec(root.right, key); return root; } public int FindMax(Node node) { if (node == null) return int.MinValue; while (node.right != null) { node = node.right; } return node.data; } } class Program { static void Main() { BST tree = new BST(); tree.Insert(30); tree.Insert(10); tree.Insert(40); tree.Insert(35); tree.Insert(50); Console.WriteLine(tree.FindMax(tree.root)); } }
Attempts:
2 left
💡 Hint
The maximum element is the rightmost node after all insertions.
✗ Incorrect
After inserting all nodes, 50 is the rightmost node and thus the maximum element.
🧠 Conceptual
expertWhy is FindMax efficient in a BST?
Why does the FindMax method in a BST run efficiently compared to searching the entire tree?
Attempts:
2 left
💡 Hint
Think about the BST property and where the maximum element is located.
✗ Incorrect
In a BST, all right children are greater than their parents, so the maximum element is the rightmost node. Traversing only right children is efficient.
