Challenge - 5 Problems
BST Floor and Ceil Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Floor Value in BST
What is the floor value of 15 in the given BST after running the code?
DSA Javascript
class Node { constructor(val) { this.val = val; this.left = null; this.right = null; } } function floorInBST(root, key) { let floor = -1; while (root) { if (root.val === key) return root.val; if (root.val > key) root = root.left; else { floor = root.val; root = root.right; } } return floor; } // BST structure // 10 // / \ // 5 20 // / \ \ // 3 7 30 const root = new Node(10); root.left = new Node(5); root.right = new Node(20); root.left.left = new Node(3); root.left.right = new Node(7); root.right.right = new Node(30); console.log(floorInBST(root, 15));
Attempts:
2 left
💡 Hint
Floor is the greatest value less than or equal to the key.
✗ Incorrect
The floor of 15 is the greatest value in the BST less than or equal to 15. Values less than 15 are 10, 5, 3, 7. The greatest among these is 10.
❓ Predict Output
intermediate2:00remaining
Output of Ceil Value in BST
What is the ceil value of 6 in the given BST after running the code?
DSA Javascript
class Node { constructor(val) { this.val = val; this.left = null; this.right = null; } } function ceilInBST(root, key) { let ceil = -1; while (root) { if (root.val === key) return root.val; if (root.val < key) root = root.right; else { ceil = root.val; root = root.left; } } return ceil; } // BST structure // 10 // / \ // 5 20 // / \ \ // 3 7 30 const root = new Node(10); root.left = new Node(5); root.right = new Node(20); root.left.left = new Node(3); root.left.right = new Node(7); root.right.right = new Node(30); console.log(ceilInBST(root, 6));
Attempts:
2 left
💡 Hint
Ceil is the smallest value greater than or equal to the key.
✗ Incorrect
The ceil of 6 is the smallest value in the BST greater than or equal to 6. Values greater than 6 are 7, 10, 20, 30. The smallest among these is 7.
🧠 Conceptual
advanced1:30remaining
Understanding Floor and Ceil in BST
Which statement correctly describes the difference between floor and ceil in a BST for a given key?
Attempts:
2 left
💡 Hint
Think about which values are less than or greater than the key.
✗ Incorrect
Floor is the greatest value in the BST that is less than or equal to the key. Ceil is the smallest value in the BST that is greater than or equal to the key.
🔧 Debug
advanced2:00remaining
Identify the Bug in Floor Function
What error will occur when running the following floor function code on a BST?
DSA Javascript
function floorInBST(root, key) {
let floor = null;
while (root) {
if (root.val === key) return root.val;
if (root.val < key) {
floor = root.val;
root = root.right;
} else {
root = root.left;
}
}
return floor;
}Attempts:
2 left
💡 Hint
Check which subtree to traverse when node value is less or greater than key.
✗ Incorrect
The traversal directions are swapped. When root.val < key, we should go right to find a larger floor candidate, not left. Similarly, when root.val > key, we should go left.
🚀 Application
expert3:00remaining
Find Floor and Ceil for Multiple Keys in BST
Given the BST and keys array [4, 14, 25], what is the combined output of floor and ceil for each key?
DSA Javascript
class Node { constructor(val) { this.val = val; this.left = null; this.right = null; } } function floorInBST(root, key) { let floor = -1; while (root) { if (root.val === key) return root.val; if (root.val > key) root = root.left; else { floor = root.val; root = root.right; } } return floor; } function ceilInBST(root, key) { let ceil = -1; while (root) { if (root.val === key) return root.val; if (root.val < key) root = root.right; else { ceil = root.val; root = root.left; } } return ceil; } // BST structure // 15 // / \ // 10 20 // / \ \ // 5 12 30 const root = new Node(15); root.left = new Node(10); root.right = new Node(20); root.left.left = new Node(5); root.left.right = new Node(12); root.right.right = new Node(30); const keys = [4, 14, 25]; const result = keys.map(k => ({ key: k, floor: floorInBST(root, k), ceil: ceilInBST(root, k) })); console.log(result);
Attempts:
2 left
💡 Hint
Check floor and ceil for each key individually using BST traversal rules.
✗ Incorrect
For key=4, floor doesn't exist (-1), ceil is 5. For 14, floor is 12 (largest ≤14), ceil is 15 (smallest ≥14). For 25, floor is 20, ceil is 30.