Challenge - 5 Problems
Master of Floor and Ceil in BST
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Floor and Ceil for a given key in BST
What is the output of the following Java code that finds floor and ceil values for key 15 in the BST?
DSA Java
class Node { int data; Node left, right; Node(int d) { data = d; left = right = null; } } public class BSTFloorCeil { Node root; int floor(Node root, int key) { int floorVal = -1; while (root != null) { if (root.data == key) return root.data; if (root.data > key) root = root.left; else { floorVal = root.data; root = root.right; } } return floorVal; } int ceil(Node root, int key) { int ceilVal = -1; while (root != null) { if (root.data == key) return root.data; if (root.data < key) root = root.right; else { ceilVal = root.data; root = root.left; } } return ceilVal; } public static void main(String[] args) { BSTFloorCeil tree = new BSTFloorCeil(); tree.root = new Node(10); tree.root.left = new Node(5); tree.root.right = new Node(20); tree.root.left.left = new Node(3); tree.root.left.right = new Node(7); tree.root.right.left = new Node(15); tree.root.right.right = new Node(30); int key = 15; System.out.println("Floor: " + tree.floor(tree.root, key)); System.out.println("Ceil: " + tree.ceil(tree.root, key)); } }
Attempts:
2 left
💡 Hint
Remember floor is the greatest value less than or equal to key, ceil is the smallest value greater than or equal to key.
✗ Incorrect
Since 15 exists in the BST, both floor and ceil for key 15 are 15 itself.
❓ Predict Output
intermediateFloor and Ceil for a key not present in BST
What is the output of floor and ceil for key 8 in the BST constructed below?
DSA Java
class Node { int data; Node left, right; Node(int d) { data = d; left = right = null; } } public class BSTFloorCeil { Node root; int floor(Node root, int key) { int floorVal = -1; while (root != null) { if (root.data == key) return root.data; if (root.data > key) root = root.left; else { floorVal = root.data; root = root.right; } } return floorVal; } int ceil(Node root, int key) { int ceilVal = -1; while (root != null) { if (root.data == key) return root.data; if (root.data < key) root = root.right; else { ceilVal = root.data; root = root.left; } } return ceilVal; } public static void main(String[] args) { BSTFloorCeil tree = new BSTFloorCeil(); tree.root = new Node(10); tree.root.left = new Node(5); tree.root.right = new Node(20); tree.root.left.left = new Node(3); tree.root.left.right = new Node(7); tree.root.right.left = new Node(15); tree.root.right.right = new Node(30); int key = 8; System.out.println("Floor: " + tree.floor(tree.root, key)); System.out.println("Ceil: " + tree.ceil(tree.root, key)); } }
Attempts:
2 left
💡 Hint
Floor is the greatest value less than or equal to key; ceil is the smallest value greater than or equal to key.
✗ Incorrect
8 is not in the BST. The greatest value less than 8 is 7, and the smallest value greater than 8 is 10.
🔧 Debug
advancedIdentify the error in floor function implementation
What error will occur when running the floor function below for key 25 in the BST?
DSA Java
int floor(Node root, int key) { int floorVal = -1; while (root != null) { if (root.data == key) return root.data; if (root.data < key) { floorVal = root.data; root = root.right; } else { root = root.left; } } return floorVal; } // BST nodes: 10, 5, 20, 15, 30 // key = 25
Attempts:
2 left
💡 Hint
Floor is the greatest value less than or equal to key. Trace the path for key 25.
✗ Incorrect
For key 25, floor is the greatest value <= 25. The function correctly updates floorVal to 20 and ends with 20.
🧠 Conceptual
advancedUnderstanding floor and ceil when key is smaller than all nodes
If the key is smaller than all nodes in a BST, what will be the floor and ceil values returned by the functions?
Attempts:
2 left
💡 Hint
Floor is the greatest value less than or equal to key; if none exists, floor is -1.
✗ Incorrect
If key is smaller than all nodes, floor does not exist (-1), ceil is the smallest node value.
🚀 Application
expertFind floor and ceil for multiple keys efficiently
Given a BST and multiple keys, which approach is most efficient to find floor and ceil for all keys?
Attempts:
2 left
💡 Hint
Think about preprocessing BST to speed up multiple queries.
✗ Incorrect
Converting BST to sorted array allows binary search for floor and ceil in O(log m) per key, efficient for multiple queries.
