Challenge - 5 Problems
BST Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BST Validation on a Simple Tree
What is the output of the following C# code that checks if a binary tree is a valid BST?
DSA C#
using System; class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class Program { static bool IsBST(Node root, int? min = null, int? max = null) { if (root == null) return true; if ((min != null && root.val <= min) || (max != null && root.val >= max)) return false; return IsBST(root.left, min, root.val) && IsBST(root.right, root.val, max); } static void Main() { Node 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.left = new Node(12); root.right.right = new Node(20); Console.WriteLine(IsBST(root)); } }
Attempts:
2 left
💡 Hint
Check if all nodes satisfy BST property: left < root < right
✗ Incorrect
The tree satisfies BST properties because every left child is less than its parent and every right child is greater. The code correctly checks this recursively.
❓ Predict Output
intermediateOutput of BST Validation on a Non-BST Tree
What is the output of the following C# code that checks if a binary tree is a valid BST?
DSA C#
using System; class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class Program { static bool IsBST(Node root, int? min = null, int? max = null) { if (root == null) return true; if ((min != null && root.val <= min) || (max != null && root.val >= max)) return false; return IsBST(root.left, min, root.val) && IsBST(root.right, root.val, max); } static void Main() { Node root = new Node(10); root.left = new Node(5); root.right = new Node(8); // Invalid: 8 < 10 but on right side root.left.left = new Node(2); root.left.right = new Node(7); Console.WriteLine(IsBST(root)); } }
Attempts:
2 left
💡 Hint
Check if right child values are greater than root
✗ Incorrect
The right child of root has value 8 which is less than 10, violating BST property. So the function returns false.
🧠 Conceptual
advancedWhy does the BST validation use min and max parameters?
In the BST validation function, why do we pass minimum and maximum allowed values during recursion?
Attempts:
2 left
💡 Hint
Think about how BST property restricts node values in subtrees
✗ Incorrect
Passing min and max helps ensure each node's value lies within the valid range defined by its ancestors, maintaining BST rules.
🔧 Debug
advancedIdentify the bug in this BST validation code
What error will this C# code produce when checking if a tree is a BST?
DSA C#
using System; class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class Program { static bool IsBST(Node root, int? min = null, int? max = null) { if (root == null) return true; if ((min != null && root.val < min) || (max != null && root.val > max)) return false; return IsBST(root.left, min, root.val) && IsBST(root.right, root.val, max); } static void Main() { Node root = new Node(10); root.left = new Node(5); root.right = new Node(15); Console.WriteLine(IsBST(root)); } }
Attempts:
2 left
💡 Hint
Check the comparison operators in the if condition
✗ Incorrect
The code uses < and > instead of <= and >=, so it incorrectly accepts invalid BSTs with equal values on boundaries.
🚀 Application
expertNumber of nodes in a valid BST after insertion
Given a valid BST with nodes 10, 5, 15, 2, 7, 12, 20, if we insert a new node with value 6, how many nodes will the BST have after insertion?
Attempts:
2 left
💡 Hint
Insertion adds one new node if value is not already present
✗ Incorrect
The BST initially has 7 nodes. Inserting a new unique value adds one node, so total becomes 8.
