Challenge - 5 Problems
Floor and Ceil Master
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 floor and ceil values for key = 15 in the given BST?
DSA C
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// BST structure:
// 20
// / \
// 10 30
// / \ \
// 5 15 40
int floorBST(struct Node* root, int key) {
int floor = -1;
while (root) {
if (root->data == key) return root->data;
if (root->data > key) root = root->left;
else {
floor = root->data;
root = root->right;
}
}
return floor;
}
int ceilBST(struct Node* root, int key) {
int ceil = -1;
while (root) {
if (root->data == key) return root->data;
if (root->data < key) root = root->right;
else {
ceil = root->data;
root = root->left;
}
}
return ceil;
}
// For key = 15, find floor and ceilAttempts:
2 left
💡 Hint
Floor is the greatest value less than or equal to the key; ceil is the smallest value greater than or equal to the key.
✗ Incorrect
Since 15 exists in the BST, both floor and ceil for key 15 are 15 itself.
❓ Predict Output
intermediateFloor and Ceil values for a key not present in BST
What are the floor and ceil values for key = 17 in the BST shown below?
// BST structure:
// 20
// / \
// 10 30
// / \ \
// 5 15 40
DSA C
int floorBST(struct Node* root, int key); int ceilBST(struct Node* root, int key); // key = 17
Attempts:
2 left
💡 Hint
Floor is the largest value less than or equal to 17; ceil is the smallest value greater than or equal to 17.
✗ Incorrect
15 is the greatest value less than 17, and 20 is the smallest value greater than 17 in the BST.
🔧 Debug
advancedIdentify the error in floorBST function
What error will occur when running this floorBST function on a BST?
DSA C
int floorBST(struct Node* root, int key) { int floor = -1; while (root != NULL) { if (root->data == key) return root->data; else if (root->data < key) { floor = root->data; root = root->right; } else { root = root->left; } } return floor; }
Attempts:
2 left
💡 Hint
Check the direction of traversal when root->data is less than key.
✗ Incorrect
When root->data < key, floor should update and traversal should go right, not left. The code goes left incorrectly causing wrong floor.
🚀 Application
advancedUsing Floor and Ceil to find closest value in BST
Given a BST and a key, which approach correctly finds the closest value to the key using floor and ceil?
Attempts:
2 left
💡 Hint
Closest value means smallest distance to key, so compare both floor and ceil.
✗ Incorrect
Both floor and ceil bracket the key. The closest is the one with the smallest absolute difference.
🧠 Conceptual
expertFloor and Ceil in BST with duplicates
In a BST that allows duplicate values (duplicates inserted to the right subtree), what is the floor and ceil of key = 15 if the BST contains nodes with values: 10, 15, 15, 20?
Attempts:
2 left
💡 Hint
Duplicates are on the right subtree, so floor and ceil for 15 should be 15.
✗ Incorrect
Since 15 exists (even duplicates), floor and ceil for 15 are both 15.
