Challenge - 5 Problems
Floor and Ceil Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output 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 after running the code?
DSA Typescript
class Node { val: number; left: Node | null; right: Node | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } function insert(root: Node | null, val: number): Node { if (!root) return new Node(val); if (val < root.val) root.left = insert(root.left, val); else root.right = insert(root.right, val); return root; } function floor(root: Node | null, key: number): number | null { let res: number | null = null; while (root) { if (root.val === key) return root.val; if (root.val > key) root = root.left; else { res = root.val; root = root.right; } } return res; } function ceil(root: Node | null, key: number): number | null { let res: number | null = null; while (root) { if (root.val === key) return root.val; if (root.val < key) root = root.right; else { res = root.val; root = root.left; } } return res; } const values = [10, 5, 18, 3, 7, 12, 20]; let root: Node | null = null; for (const v of values) { root = insert(root, v); } const key = 15; const floorVal = floor(root, key); const ceilVal = ceil(root, key); console.log(`Floor: ${floorVal}, Ceil: ${ceilVal}`);
Attempts:
2 left
💡 Hint
Trace the BST traversal for floor and ceil separately, comparing node values with the key.
✗ Incorrect
The floor is the greatest value less than or equal to the key. For 15, the floor is 12.
The ceil is the smallest value greater than or equal to the key. For 15, the ceil is 18.
🧠 Conceptual
intermediate1:00remaining
Number of nodes visited in floor search
In the worst case, how many nodes does the floor function visit in a BST with n nodes?
Attempts:
2 left
💡 Hint
Consider the shape of the BST in the worst case (like a linked list).
✗ Incorrect
In the worst case, the BST is skewed, so floor function visits all nodes along the path, which can be O(n).
🔧 Debug
advanced1:30remaining
Identify the error in ceil function implementation
What error will occur when running this ceil function on a BST if the key is larger than all nodes?
DSA Typescript
function ceil(root: Node | null, key: number): number {
let res: number | null = null;
while (root) {
if (root.val === key) return root.val;
if (root.val < key) root = root.right;
else {
res = root.val;
root = root.left;
}
}
return res!;
}Attempts:
2 left
💡 Hint
What happens if res remains null and you use the non-null assertion operator?
✗ Incorrect
If the key is larger than all nodes, res stays null. Using res! forces a non-null return, causing a runtime error.
❓ Predict Output
advanced2:00remaining
Output after inserting nodes and finding floor and ceil
What is the output after inserting nodes [8, 4, 12, 2, 6, 10, 14] and finding floor and ceil for key 5?
DSA Typescript
class Node { val: number; left: Node | null; right: Node | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } function insert(root: Node | null, val: number): Node { if (!root) return new Node(val); if (val < root.val) root.left = insert(root.left, val); else root.right = insert(root.right, val); return root; } function floor(root: Node | null, key: number): number | null { let res: number | null = null; while (root) { if (root.val === key) return root.val; if (root.val > key) root = root.left; else { res = root.val; root = root.right; } } return res; } function ceil(root: Node | null, key: number): number | null { let res: number | null = null; while (root) { if (root.val === key) return root.val; if (root.val < key) root = root.right; else { res = root.val; root = root.left; } } return res; } const values = [8, 4, 12, 2, 6, 10, 14]; let root: Node | null = null; for (const v of values) { root = insert(root, v); } const key = 5; const floorVal = floor(root, key); const ceilVal = ceil(root, key); console.log(`Floor: ${floorVal}, Ceil: ${ceilVal}`);
Attempts:
2 left
💡 Hint
Check nodes less than and greater than 5 in the BST.
✗ Incorrect
Floor is the greatest value less than or equal to 5, which is 4.
Ceil is the smallest value greater than or equal to 5, which is 6.
🧠 Conceptual
expert1:30remaining
Effect of BST shape on floor and ceil efficiency
How does the shape of a BST affect the efficiency of floor and ceil operations?
Attempts:
2 left
💡 Hint
Consider the height of the tree and path length for search.
✗ Incorrect
Balanced BST has height about log n, so floor and ceil run in O(log n).
Skewed BST is like a linked list, so operations degrade to O(n).