Challenge - 5 Problems
Floor and Ceil Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find floor and ceil of 7 in a sorted array
What is the output of the following code that finds the floor and ceil of 7 in the sorted array [1, 3, 5, 7, 9, 11]?
DSA Javascript
function floorAndCeil(arr, x) {
let floor = -1, ceil = -1;
let left = 0, right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === x) {
floor = arr[mid];
ceil = arr[mid];
break;
} else if (arr[mid] < x) {
floor = arr[mid];
left = mid + 1;
} else {
ceil = arr[mid];
right = mid - 1;
}
}
return { floor, ceil };
}
console.log(floorAndCeil([1, 3, 5, 7, 9, 11], 7));Attempts:
2 left
💡 Hint
Remember floor is the greatest element less than or equal to x, ceil is the smallest element greater than or equal to x.
✗ Incorrect
Since 7 is present in the array, both floor and ceil are 7.
❓ Predict Output
intermediate2:00remaining
Floor and ceil for 8 in a sorted array
What is the output of the code that finds floor and ceil of 8 in the sorted array [2, 4, 6, 10, 12]?
DSA Javascript
function floorAndCeil(arr, x) {
let floor = -1, ceil = -1;
let left = 0, right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === x) {
floor = arr[mid];
ceil = arr[mid];
break;
} else if (arr[mid] < x) {
floor = arr[mid];
left = mid + 1;
} else {
ceil = arr[mid];
right = mid - 1;
}
}
return { floor, ceil };
}
console.log(floorAndCeil([2, 4, 6, 10, 12], 8));Attempts:
2 left
💡 Hint
8 is not in the array, so floor and ceil are neighbors around 8.
✗ Incorrect
Floor is the greatest element less than 8, which is 6. Ceil is the smallest element greater than 8, which is 10.
🧠 Conceptual
advanced1:30remaining
Floor and Ceil when element is smaller than all array elements
If you search for floor and ceil of 1 in the sorted array [3, 5, 7, 9], what will be the floor and ceil values?
Attempts:
2 left
💡 Hint
Floor is the greatest element less than or equal to x. If none exists, floor is -1.
✗ Incorrect
Since 1 is less than all elements, floor is -1 (no smaller or equal element), ceil is the smallest element 3.
🧠 Conceptual
advanced1:30remaining
Floor and Ceil when element is greater than all array elements
What are the floor and ceil of 20 in the sorted array [2, 4, 6, 8, 10]?
Attempts:
2 left
💡 Hint
Ceil is the smallest element greater than or equal to x. If none exists, ceil is -1.
✗ Incorrect
20 is greater than all elements, so floor is the greatest element 10, ceil is -1 (no greater or equal element).
🔧 Debug
expert2:30remaining
Identify the error in floor and ceil implementation
What error will the following code produce when finding floor and ceil of 5 in [1, 2, 3, 4]?
DSA Javascript
function floorAndCeil(arr, x) {
let floor = -1, ceil = -1;
let left = 0, right = arr.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === x) {
floor = arr[mid];
ceil = arr[mid];
break;
} else if (arr[mid] < x) {
floor = arr[mid];
left = mid + 1;
} else {
ceil = arr[mid];
right = mid - 1;
}
}
return { floor, ceil };
}
console.log(floorAndCeil([1, 2, 3, 4], 5));Attempts:
2 left
💡 Hint
Check the loop condition and how left and right pointers move.
✗ Incorrect
The loop condition 'left < right' causes infinite loop when searching for a value greater than all elements because left and right pointers do not converge properly.