Challenge - 5 Problems
BST Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of the BST after inserting 15?
Given the BST below, what is the printed state after inserting the value 15?
Initial BST (in-order): 10 -> 20 -> 30 -> null
Initial BST (in-order): 10 -> 20 -> 30 -> null
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } class BST { Node root; void insert(int val) { root = insertRec(root, val); } Node insertRec(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = insertRec(root.left, val); else root.right = insertRec(root.right, val); return root; } void inorder(Node root) { if (root != null) { inorder(root.left); System.out.print(root.val + " -> "); inorder(root.right); } } } public class Main { public static void main(String[] args) { BST tree = new BST(); tree.insert(20); tree.insert(10); tree.insert(30); tree.insert(15); tree.inorder(tree.root); System.out.print("null"); } }
Attempts:
2 left
💡 Hint
Remember that in-order traversal prints nodes in ascending order for BST.
✗ Incorrect
In-order traversal of a BST prints values in sorted order. After inserting 15, the BST contains 10, 15, 20, 30. So the output is 10 -> 15 -> 20 -> 30 -> null.
❓ Predict Output
intermediateWhat is the output after inserting 5 into the BST?
Given the BST with nodes 10, 20, 30, what is the printed state after inserting 5?
Initial BST (in-order): 10 -> 20 -> 30 -> null
Initial BST (in-order): 10 -> 20 -> 30 -> null
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } class BST { Node root; void insert(int val) { root = insertRec(root, val); } Node insertRec(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = insertRec(root.left, val); else root.right = insertRec(root.right, val); return root; } void inorder(Node root) { if (root != null) { inorder(root.left); System.out.print(root.val + " -> "); inorder(root.right); } } } public class Main { public static void main(String[] args) { BST tree = new BST(); tree.insert(20); tree.insert(10); tree.insert(30); tree.insert(5); tree.inorder(tree.root); System.out.print("null"); } }
Attempts:
2 left
💡 Hint
In-order traversal prints nodes in ascending order.
✗ Incorrect
After inserting 5, the BST contains 5, 10, 20, 30. In-order traversal prints them sorted: 5 -> 10 -> 20 -> 30 -> null.
🔧 Debug
advancedWhat error occurs when inserting duplicate values in this BST insert method?
Consider the insert method below. What happens if we insert a value already present in the BST?
DSA Java
Node insertRec(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = insertRec(root.left, val); else root.right = insertRec(root.right, val); return root; }
Attempts:
2 left
💡 Hint
Look at the condition for deciding left or right subtree insertion.
✗ Incorrect
The code inserts duplicates in the right subtree because val == root.val goes to else branch. No error occurs, duplicates are allowed on right.
❓ Predict Output
advancedWhat is the output after inserting 25 into this BST?
Given the BST with nodes 20, 10, 30, 25, what is the printed state after inserting 25 again?
Initial BST (in-order): 10 -> 20 -> 25 -> 30 -> null
Initial BST (in-order): 10 -> 20 -> 25 -> 30 -> null
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } class BST { Node root; void insert(int val) { root = insertRec(root, val); } Node insertRec(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = insertRec(root.left, val); else root.right = insertRec(root.right, val); return root; } void inorder(Node root) { if (root != null) { inorder(root.left); System.out.print(root.val + " -> "); inorder(root.right); } } } public class Main { public static void main(String[] args) { BST tree = new BST(); tree.insert(20); tree.insert(10); tree.insert(30); tree.insert(25); tree.insert(25); tree.inorder(tree.root); System.out.print("null"); } }
Attempts:
2 left
💡 Hint
Duplicates are inserted to the right subtree.
✗ Incorrect
Inserting 25 twice adds two nodes with value 25. In-order traversal prints duplicates in sorted order.
🧠 Conceptual
expertHow many nodes are in the BST after inserting these values: 50, 30, 70, 20, 40, 60, 80, 70?
You insert the values 50, 30, 70, 20, 40, 60, 80, and then 70 again into an empty BST using the insert method below. How many nodes does the BST contain after all insertions?
DSA Java
Node insertRec(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = insertRec(root.left, val); else root.right = insertRec(root.right, val); return root; }
Attempts:
2 left
💡 Hint
Duplicates are inserted as new nodes in the right subtree.
✗ Incorrect
All values including the duplicate 70 are inserted as nodes. So total nodes = 8.
