Challenge - 5 Problems
BST Minimum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind minimum element in a BST - Output?
What is the output of the following C# code that finds the minimum 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 FindMin(Node node) { if (node == null) throw new InvalidOperationException("Tree is empty"); while (node.left != null) { node = node.left; } return node.data; } } class Program { static void Main() { BST tree = new BST(); tree.root = new Node(20); tree.root.left = new Node(10); tree.root.right = new Node(30); tree.root.left.left = new Node(5); tree.root.left.right = new Node(15); Console.WriteLine(tree.FindMin(tree.root)); } }
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
✗ Incorrect
The code traverses the left children starting from the root until it reaches the leftmost node, which holds the minimum value 5.
❓ Predict Output
intermediateWhat error occurs when finding minimum in an empty BST?
What error does the following C# code throw when trying to find the minimum element in an empty 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 FindMin(Node node) { if (node == null) throw new InvalidOperationException("Tree is empty"); while (node.left != null) { node = node.left; } return node.data; } } class Program { static void Main() { BST tree = new BST(); Console.WriteLine(tree.FindMin(tree.root)); } }
Attempts:
2 left
💡 Hint
Check the explicit exception thrown when node is null.
✗ Incorrect
The code explicitly throws InvalidOperationException with message "Tree is empty" when the root node is null.
🔧 Debug
advancedWhy does this FindMin method cause an infinite loop?
Identify the reason for the infinite loop in the following FindMin method for a BST.
DSA C#
public int FindMin(Node node) { while (node.left != null) { node = node.right; } return node.data; }
Attempts:
2 left
💡 Hint
The loop condition checks left child but moves to right child inside the loop.
✗ Incorrect
The loop condition checks if node.left is not null but inside the loop it moves to node.right, so node.left never changes and the loop never ends.
🧠 Conceptual
advancedMinimum element in BST after insertion
Given a BST with minimum element 3, what will be the minimum element after inserting 1 and 4?
Attempts:
2 left
💡 Hint
In BST, smaller values go to the left subtree.
✗ Incorrect
Inserting 1, which is smaller than current minimum 3, will update the minimum element to 1.
🚀 Application
expertFind minimum element in BST with duplicates allowed
In a BST that allows duplicate values inserted to the right subtree, what is the minimum element after inserting these values in order: 10, 5, 5, 3, 7?
Attempts:
2 left
💡 Hint
Duplicates go to the right subtree, minimum is still the leftmost node.
✗ Incorrect
Even with duplicates inserted to the right, the minimum element is the leftmost node which is 3.
