Challenge - 5 Problems
BST Floor and Ceil Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind floor value in BST
What is the floor value of 15 in the given BST after inserting the values [10, 5, 15, 12, 18]?
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 floor_bst(root, key): floor_val = -1 while root: if root.val == key: return root.val if root.val > key: root = root.left else: floor_val = root.val root = root.right return floor_val root = None for v in [10, 5, 15, 12, 18]: root = insert(root, v) print(floor_bst(root, 15))
Attempts:
2 left
💡 Hint
Floor is the greatest value less than or equal to the key.
✗ Incorrect
The floor of 15 is 15 itself because 15 exists in the BST.
❓ Predict Output
intermediateFind ceil value in BST
What is the ceil value of 14 in the BST after inserting [20, 8, 22, 4, 12, 10, 14]?
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 ceil_bst(root, key): ceil_val = -1 while root: if root.val == key: return root.val if root.val < key: root = root.right else: ceil_val = root.val root = root.left return ceil_val root = None for v in [20, 8, 22, 4, 12, 10, 14]: root = insert(root, v) print(ceil_bst(root, 14))
Attempts:
2 left
💡 Hint
Ceil is the smallest value greater than or equal to the key.
✗ Incorrect
14 exists in the BST, so ceil is 14 itself.
🧠 Conceptual
advancedFloor and Ceil when key not present
In a BST with values [5, 10, 15, 20, 25], what are the floor and ceil values for key = 17?
Attempts:
2 left
💡 Hint
Floor is largest smaller or equal, ceil is smallest greater or equal.
✗ Incorrect
17 is between 15 and 20, so floor is 15 and ceil is 20.
🔧 Debug
advancedIdentify error in floor function
What error will this floor function produce when called with key=7 on BST with [5, 10, 15]?
```python
def floor_bst(root, key):
floor_val = None
while root:
if root.val == key:
return root.val
if root.val < key:
floor_val = root.val
root = root.left
else:
root = root.right
return floor_val
```
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 root = None for v in [5, 10, 15]: root = insert(root, v) print(floor_bst(root, 7))
Attempts:
2 left
💡 Hint
Check the direction of traversal when root.val < key.
✗ Incorrect
The code incorrectly moves left when root.val < key, missing larger values on right. For key=7, it returns None.
🚀 Application
expertCount nodes between floor and ceil in BST
Given a BST with values [8, 3, 10, 1, 6, 14, 4, 7, 13], how many nodes have values between floor(5) and ceil(11) inclusive?
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 floor_bst(root, key): floor_val = -1 while root: if root.val == key: return root.val if root.val > key: root = root.left else: floor_val = root.val root = root.right return floor_val def ceil_bst(root, key): ceil_val = -1 while root: if root.val == key: return root.val if root.val < key: root = root.right else: ceil_val = root.val root = root.left return ceil_val def count_in_range(root, low, high): if root is None: return 0 if root.val < low: return count_in_range(root.right, low, high) if root.val > high: return count_in_range(root.left, low, high) return 1 + count_in_range(root.left, low, high) + count_in_range(root.right, low, high) root = None for v in [8, 3, 10, 1, 6, 14, 4, 7, 13]: root = insert(root, v) floor_val = floor_bst(root, 5) ceil_val = ceil_bst(root, 11) print(count_in_range(root, floor_val, ceil_val))
Attempts:
2 left
💡 Hint
Find floor and ceil first, then count nodes in that range.
✗ Incorrect
Floor(5) = 4, Ceil(11) = 13. Nodes in [4,13] are 4,6,7,8,10,13 → 6 nodes.
