Challenge - 5 Problems
Floor and Ceil Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind floor and ceil of 5 in the array
Given the sorted array
int[] arr = {1, 2, 4, 6, 8, 10};, what is the output of the floor and ceil values for key = 5?DSA C#
int Floor(int[] arr, int key) { int left = 0, right = arr.Length - 1; int floor = -1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == key) return arr[mid]; if (arr[mid] < key) { floor = arr[mid]; left = mid + 1; } else { right = mid - 1; } } return floor; } int Ceil(int[] arr, int key) { int left = 0, right = arr.Length - 1; int ceil = -1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == key) return arr[mid]; if (arr[mid] > key) { ceil = arr[mid]; right = mid - 1; } else { left = mid + 1; } } return ceil; } int[] arr = {1, 2, 4, 6, 8, 10}; int key = 5; int floorValue = Floor(arr, key); int ceilValue = Ceil(arr, key); Console.WriteLine($"Floor: {floorValue}, Ceil: {ceilValue}");
Attempts:
2 left
💡 Hint
Remember floor is the greatest element less than or equal to key, ceil is the smallest element greater than or equal to key.
✗ Incorrect
The floor of 5 is 4 because 4 is the greatest number less than 5 in the array. The ceil of 5 is 6 because 6 is the smallest number greater than 5 in the array.
❓ Predict Output
intermediateFloor and Ceil for key smaller than all elements
What will be the output of floor and ceil for
key = 0 in the sorted array int[] arr = {3, 5, 7, 9};?DSA C#
int Floor(int[] arr, int key) { int left = 0, right = arr.Length - 1; int floor = -1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == key) return arr[mid]; if (arr[mid] < key) { floor = arr[mid]; left = mid + 1; } else { right = mid - 1; } } return floor; } int Ceil(int[] arr, int key) { int left = 0, right = arr.Length - 1; int ceil = -1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == key) return arr[mid]; if (arr[mid] > key) { ceil = arr[mid]; right = mid - 1; } else { left = mid + 1; } } return ceil; } int[] arr = {3, 5, 7, 9}; int key = 0; int floorValue = Floor(arr, key); int ceilValue = Ceil(arr, key); Console.WriteLine($"Floor: {floorValue}, Ceil: {ceilValue}");
Attempts:
2 left
💡 Hint
If key is smaller than all elements, floor should be -1 (no floor), ceil should be the smallest element.
✗ Incorrect
Since 0 is smaller than all elements, floor does not exist (-1). The ceil is the smallest element 3.
🔧 Debug
advancedIdentify the bug in floor function
Consider the following floor function for a sorted array. What is the bug that causes incorrect floor value for some inputs?
int Floor(int[] arr, int key) {
int left = 0, right = arr.Length - 1;
int floor = -1;
while (left < right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) return arr[mid];
if (arr[mid] < key) {
floor = arr[mid];
left = mid + 1;
} else {
right = mid - 1;
}
}
return floor;
}Attempts:
2 left
💡 Hint
Check if the loop covers all elements including when left equals right.
✗ Incorrect
Using 'left < right' skips the case when left == right, potentially missing the correct floor. Changing to 'left <= right' ensures all elements are checked.
🧠 Conceptual
advancedFloor and Ceil when key is present in array
If the key is present in the sorted array, what will be the floor and ceil values returned by the standard binary search based floor and ceil functions?
Attempts:
2 left
💡 Hint
Think about what floor and ceil mean when the key exists exactly in the array.
✗ Incorrect
If the key is found, both floor and ceil are the key itself because floor is the greatest element less than or equal to key, and ceil is the smallest element greater than or equal to key.
❓ Predict Output
expertFloor and Ceil for large array with duplicates
Given the sorted array
int[] arr = {1, 2, 2, 2, 3, 4, 5, 5, 6};, what is the output of floor and ceil for key = 2?DSA C#
int Floor(int[] arr, int key) { int left = 0, right = arr.Length - 1; int floor = -1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == key) return arr[mid]; if (arr[mid] < key) { floor = arr[mid]; left = mid + 1; } else { right = mid - 1; } } return floor; } int Ceil(int[] arr, int key) { int left = 0, right = arr.Length - 1; int ceil = -1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == key) return arr[mid]; if (arr[mid] > key) { ceil = arr[mid]; right = mid - 1; } else { left = mid + 1; } } return ceil; } int[] arr = {1, 2, 2, 2, 3, 4, 5, 5, 6}; int key = 2; int floorValue = Floor(arr, key); int ceilValue = Ceil(arr, key); Console.WriteLine($"Floor: {floorValue}, Ceil: {ceilValue}");
Attempts:
2 left
💡 Hint
When key is present multiple times, floor and ceil should return the key itself.
✗ Incorrect
Since 2 is present in the array, both floor and ceil return 2 regardless of duplicates.
