Challenge - 5 Problems
BST Minimum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the minimum element in a BST
What is the output of the code when finding the minimum element in the given BST?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def find_min(root): current = root while current.left is not None: current = current.left return current.val root = Node(10) root.left = Node(5) root.right = Node(15) root.left.left = Node(2) root.left.right = Node(7) root.left.left.left = Node(1) print(find_min(root))
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
✗ Incorrect
The function moves left until it finds the leftmost node, which has the smallest value. Here, that value is 1.
❓ Predict Output
intermediateMinimum element in a BST with only right children
What will the output be when finding the minimum element in this BST where all nodes only have right children?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def find_min(root): current = root while current.left is not None: current = current.left return current.val root = Node(3) root.right = Node(5) root.right.right = Node(7) print(find_min(root))
Attempts:
2 left
💡 Hint
If there is no left child, the root is the minimum.
✗ Incorrect
Since there are no left children, the root node value 3 is the smallest.
❓ Predict Output
advancedFind minimum element after inserting nodes
After inserting nodes into the BST, what is the minimum element found?
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 find_min(root): current = root while current.left is not None: current = current.left return current.val root = None for v in [20, 10, 30, 5, 15, 3]: root = insert(root, v) print(find_min(root))
Attempts:
2 left
💡 Hint
The smallest inserted value is the minimum.
✗ Incorrect
The smallest value inserted is 3, which is the leftmost node.
🔧 Debug
advancedIdentify the error in this minimum element finder
What error will this code raise when trying to find the minimum element in a BST?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def find_min(root): current = root while current.left: current = current.right return current.val root = Node(10) root.left = Node(5) root.left.left = Node(2) print(find_min(root))
Attempts:
2 left
💡 Hint
Check the direction of traversal inside the loop.
✗ Incorrect
The code moves to the right child while checking for a left child. Since the root has a left child but no right child, current becomes None, and evaluating current.left raises AttributeError.
🧠 Conceptual
expertWhy does finding the minimum element in a BST take O(h) time?
Choose the best explanation for why finding the minimum element in a BST takes time proportional to the tree height h.
Attempts:
2 left
💡 Hint
Think about the path to the smallest value in a BST.
✗ Incorrect
The minimum element is found by going left from the root until no more left child exists, which can be up to height h.
