Challenge - 5 Problems
BST Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting nodes in BST
What is the printed state of the BST after inserting the values 10, 5, 15, 3, 7 in that order? The BST is printed in-order (left -> root -> right).
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def insert(root, val): if root is None: return Node(val) if val < root.val: root.left = insert(root.left, val) else: root.right = insert(root.right, val) return root def inorder(root): return inorder(root.left) + [root.val] + inorder(root.right) if root else [] root = None for v in [10, 5, 15, 3, 7]: root = insert(root, v) print(inorder(root))
Attempts:
2 left
💡 Hint
Remember that in-order traversal of a BST prints values in sorted order.
✗ Incorrect
Inserting nodes 10, 5, 15, 3, 7 creates a BST where in-order traversal visits nodes in ascending order: 3, 5, 7, 10, 15.
❓ Predict Output
intermediateOutput after inserting duplicate value in BST
What is the in-order traversal output after inserting values 20, 10, 30, 20 into an empty BST? Assume duplicates go to the right subtree.
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def insert(root, val): if root is None: return Node(val) if val < root.val: root.left = insert(root.left, val) else: root.right = insert(root.right, val) return root def inorder(root): return inorder(root.left) + [root.val] + inorder(root.right) if root else [] root = None for v in [20, 10, 30, 20]: root = insert(root, v) print(inorder(root))
Attempts:
2 left
💡 Hint
Duplicates are inserted to the right subtree, so the second 20 appears after the first in in-order.
✗ Incorrect
The second 20 is inserted to the right of the first 20, so in-order traversal lists both 20s in order: 10, 20, 20, 30.
🔧 Debug
advancedIdentify the error in BST insert function
What error will this BST insert function cause when inserting values 8, 3, 10, 1, 6?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def insert(root, val): if root is None: return Node(val) if val <= root.val: insert(root.left, val) else: insert(root.right, val) return root root = None for v in [8, 3, 10, 1, 6]: root = insert(root, v) # Print in-order traversal def inorder(root): return inorder(root.left) + [root.val] + inorder(root.right) if root else [] print(inorder(root))
Attempts:
2 left
💡 Hint
Check if recursive calls update the left or right child pointers.
✗ Incorrect
The insert function does not assign the recursive insert result back to root.left or root.right, so the tree does not grow beyond the root node. The in-order traversal prints only [8].
❓ Predict Output
advancedBST structure after complex insertions
After inserting values [50, 30, 70, 20, 40, 60, 80] into an empty BST, what is the pre-order traversal output (root -> left -> right)?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def insert(root, val): if root is None: return Node(val) if val < root.val: root.left = insert(root.left, val) else: root.right = insert(root.right, val) return root def preorder(root): return [root.val] + preorder(root.left) + preorder(root.right) if root else [] root = None for v in [50, 30, 70, 20, 40, 60, 80]: root = insert(root, v) print(preorder(root))
Attempts:
2 left
💡 Hint
Pre-order traversal visits root first, then left subtree, then right subtree.
✗ Incorrect
The BST structure after insertion has root 50, left subtree with 30, 20, 40 and right subtree with 70, 60, 80. Pre-order visits root, left subtree in pre-order, then right subtree in pre-order.
🧠 Conceptual
expertNumber of nodes after multiple insertions in BST
If you insert the values [15, 10, 20, 10, 25, 15, 5] into an empty BST where duplicates are inserted to the right subtree, how many nodes will the BST contain?
Attempts:
2 left
💡 Hint
Duplicates are inserted as new nodes to the right subtree, so they increase the node count.
✗ Incorrect
All values including duplicates are inserted as separate nodes. The list has 7 values, so the BST will have 7 nodes.
