Challenge - 5 Problems
BST Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Inserting Nodes in a BST
What is the printed state of the BST after inserting the following values in order: 10, 5, 15, 3, 7?
DSA C#
class Node { public int Value; public Node Left, Right; public Node(int value) { Value = value; Left = Right = null; } } class BST { public Node Root; public void Insert(int value) { Root = InsertRec(Root, value); } private Node InsertRec(Node root, int value) { if (root == null) return new Node(value); if (value < root.Value) root.Left = InsertRec(root.Left, value); else root.Right = InsertRec(root.Right, value); return root; } public void PrintInOrder(Node root) { if (root == null) return; PrintInOrder(root.Left); System.Console.Write(root.Value + " -> "); PrintInOrder(root.Right); } } var tree = new BST(); tree.Insert(10); tree.Insert(5); tree.Insert(15); tree.Insert(3); tree.Insert(7); tree.PrintInOrder(tree.Root); System.Console.WriteLine("null");
Attempts:
2 left
💡 Hint
Remember that in-order traversal of a BST prints values in ascending order.
✗ Incorrect
In-order traversal visits left subtree, then node, then right subtree. For a BST, this prints values sorted ascending.
🧠 Conceptual
intermediateWhy BST Property is Important for Search Efficiency
Why does the BST property (left child < node < right child) help in efficient searching?
Attempts:
2 left
💡 Hint
Think about how binary search works on sorted data.
✗ Incorrect
The BST property lets us decide to go left or right subtree based on comparison, halving the search space each step.
🔧 Debug
advancedIdentify the Error in BST Insertion Code
What error will this BST insertion code cause when inserting duplicate values?
class BST {
public Node Root;
public void Insert(int value) {
Root = InsertRec(Root, value);
}
private Node InsertRec(Node root, int value) {
if (root == null) return new Node(value);
if (value < root.Value) root.Left = InsertRec(root.Left, value);
else root.Right = InsertRec(root.Right, value);
return root;
}
}
Attempts:
2 left
💡 Hint
Check how the code handles values equal to the current node.
✗ Incorrect
The code inserts duplicates to the right subtree, which can cause the tree to become skewed if many duplicates exist.
❓ Predict Output
advancedOutput After Deleting a Node with Two Children in BST
Given this BST and deletion of node 10, what is the in-order traversal output?
Insert order: 10, 5, 15, 3, 7, 12, 18
Delete node: 10
Deletion replaces node with its in-order successor.
DSA C#
class Node { public int Value; public Node Left, Right; public Node(int value) { Value = value; Left = Right = null; } } class BST { public Node Root; public void Insert(int value) { Root = InsertRec(Root, value); } private Node InsertRec(Node root, int value) { if (root == null) return new Node(value); if (value < root.Value) root.Left = InsertRec(root.Left, value); else root.Right = InsertRec(root.Right, value); return root; } public void Delete(int value) { Root = DeleteRec(Root, value); } private Node DeleteRec(Node root, int value) { if (root == null) return root; if (value < root.Value) root.Left = DeleteRec(root.Left, value); else if (value > root.Value) root.Right = DeleteRec(root.Right, value); else { if (root.Left == null) return root.Right; else if (root.Right == null) return root.Left; root.Value = MinValue(root.Right); root.Right = DeleteRec(root.Right, root.Value); } return root; } private int MinValue(Node root) { int minv = root.Value; while (root.Left != null) { root = root.Left; minv = root.Value; } return minv; } public void PrintInOrder(Node root) { if (root == null) return; PrintInOrder(root.Left); System.Console.Write(root.Value + " -> "); PrintInOrder(root.Right); } } var tree = new BST(); tree.Insert(10); tree.Insert(5); tree.Insert(15); tree.Insert(3); tree.Insert(7); tree.Insert(12); tree.Insert(18); tree.Delete(10); tree.PrintInOrder(tree.Root); System.Console.WriteLine("null");
Attempts:
2 left
💡 Hint
The in-order successor is the smallest value in the right subtree.
✗ Incorrect
Node 10 is replaced by 12 (its in-order successor). The tree then has 12 in place of 10, and 12 is removed from its original position.
🧠 Conceptual
expertWhy BST Can Degenerate and How to Prevent It
Which statement best explains why a BST can become inefficient and how to prevent it?
Attempts:
2 left
💡 Hint
Think about what happens when inserting sorted data into a BST.
✗ Incorrect
Inserting sorted data creates a skewed tree like a linked list, losing efficiency. Balanced BSTs use rotations to keep height low.
