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 nodes 10, 5, 20, 12, 30?
DSA C#
class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public void Insert(int val) { root = InsertRec(root, val); } private 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; } public int Floor(int key) { Node res = null; Node cur = root; while (cur != null) { if (cur.val == key) return cur.val; if (cur.val > key) cur = cur.left; else { res = cur; cur = cur.right; } } return res == null ? -1 : res.val; } } var bst = new BST(); bst.Insert(10); bst.Insert(5); bst.Insert(20); bst.Insert(12); bst.Insert(30); int floorValue = bst.Floor(15); System.Console.WriteLine(floorValue);
Attempts:
2 left
💡 Hint
Floor is the greatest value less than or equal to the key in BST.
✗ Incorrect
The floor of 15 is the greatest value in the BST less than or equal to 15. Nodes less than 15 are 10, 5, 12. Among these, 12 is the greatest.
❓ Predict Output
intermediateFind Ceil Value in BST
What is the ceil value of 13 in the BST after inserting nodes 8, 4, 10, 14, 20?
DSA C#
class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public void Insert(int val) { root = InsertRec(root, val); } private 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; } public int Ceil(int key) { Node res = null; Node cur = root; while (cur != null) { if (cur.val == key) return cur.val; if (cur.val < key) cur = cur.right; else { res = cur; cur = cur.left; } } return res == null ? -1 : res.val; } } var bst = new BST(); bst.Insert(8); bst.Insert(4); bst.Insert(10); bst.Insert(14); bst.Insert(20); int ceilValue = bst.Ceil(13); System.Console.WriteLine(ceilValue);
Attempts:
2 left
💡 Hint
Ceil is the smallest value greater than or equal to the key in BST.
✗ Incorrect
The ceil of 13 is the smallest value in the BST greater than or equal to 13. Nodes greater than 13 are 14 and 20. Among these, 14 is the smallest.
🔧 Debug
advancedIdentify the Bug in Floor Function
What error will occur when running this Floor function code on a BST?
DSA C#
public int Floor(Node root, int key) { if (root == null) return -1; if (root.val == key) return root.val; if (root.val > key) return Floor(root.left, key); int floorValue = Floor(root.right, key); return floorValue >= key ? floorValue : root.val; }
Attempts:
2 left
💡 Hint
Check the condition comparing floorValue and key before returning.
✗ Incorrect
The condition floorValue >= key is wrong. It should be floorValue <= key to ensure floorValue is less than or equal to key. This bug causes incorrect floor values.
❓ Predict Output
advancedFloor and Ceil for Non-Existing Key
Given a BST with nodes 7, 3, 9, 1, 5, what are the floor and ceil values for key 6?
DSA C#
class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public void Insert(int val) { root = InsertRec(root, val); } private 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; } public int Floor(int key) { Node res = null; Node cur = root; while (cur != null) { if (cur.val == key) return cur.val; if (cur.val > key) cur = cur.left; else { res = cur; cur = cur.right; } } return res == null ? -1 : res.val; } public int Ceil(int key) { Node res = null; Node cur = root; while (cur != null) { if (cur.val == key) return cur.val; if (cur.val < key) cur = cur.right; else { res = cur; cur = cur.left; } } return res == null ? -1 : res.val; } } var bst = new BST(); bst.Insert(7); bst.Insert(3); bst.Insert(9); bst.Insert(1); bst.Insert(5); int floorValue = bst.Floor(6); int ceilValue = bst.Ceil(6); System.Console.WriteLine($"Floor: {floorValue}, Ceil: {ceilValue}");
Attempts:
2 left
💡 Hint
Floor is greatest smaller or equal, ceil is smallest greater or equal.
✗ Incorrect
6 is not in BST. Floor is greatest value less than 6, which is 5. Ceil is smallest value greater than 6, which is 7.
🧠 Conceptual
expertTime Complexity of Floor and Ceil in BST
What is the worst-case time complexity of finding floor or ceil in a BST with n nodes?
Attempts:
2 left
💡 Hint
Consider BST shape impact on search path length.
✗ Incorrect
In worst case, BST is skewed (like a linked list), so search takes O(n). If balanced, search takes O(log n).
