Challenge - 5 Problems
BST Maximum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind maximum element in a BST
What is the output of the following code that finds the maximum element in a Binary Search Tree (BST)?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def find_max(root): current = root while current.right: current = current.right return current.val # Construct BST root = Node(10) root.left = Node(5) root.right = Node(15) root.right.right = Node(20) root.right.left = Node(12) print(find_max(root))
Attempts:
2 left
💡 Hint
In a BST, the maximum element is the rightmost node.
✗ Incorrect
The function moves to the right child repeatedly until it reaches the rightmost node, which holds the maximum value 20.
❓ Predict Output
intermediateOutput of find_max with single node BST
What will be printed when find_max is called on a BST with only one node?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def find_max(root): current = root while current.right: current = current.right return current.val root = Node(42) print(find_max(root))
Attempts:
2 left
💡 Hint
If there is no right child, the root itself is the maximum.
✗ Incorrect
Since the BST has only one node with value 42 and no right child, the function returns 42.
🔧 Debug
advancedIdentify the error in find_max function
What error will this code raise when trying to find the maximum element in a BST?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def find_max(root): current = root while current.right is not None: current = current.right return current.val root = Node(10) root.right = Node(20) print(find_max(root))
Attempts:
2 left
💡 Hint
Check the direction of traversal inside the loop.
✗ Incorrect
The original code incorrectly moved left inside the loop. The corrected code moves right until no right child exists, returning 20.
🧠 Conceptual
advancedMaximum element in an empty BST
What happens if you call find_max on an empty BST (root is None)?
DSA Python
def find_max(root): current = root while current.right: current = current.right return current.val print(find_max(None))
Attempts:
2 left
💡 Hint
Consider what happens when current is None and you try to access current.right.
✗ Incorrect
Since root is None, current is None, and accessing current.right raises AttributeError.
🚀 Application
expertFind maximum element in BST with parent pointers
Given a BST node class with parent pointers, which code snippet correctly finds the maximum element starting from any node in the tree?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None self.parent = None # Assume 'node' is any node in the BST
Attempts:
2 left
💡 Hint
Maximum element is the rightmost node in the BST subtree rooted at the given node.
✗ Incorrect
To find maximum from any node, move right until no right child exists. Parent pointers are not needed for this operation.
