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 for a value in a sorted array
What is the output of the following code that finds the floor and ceil of 7 in the sorted array?
DSA C
int arr[] = {1, 3, 5, 6, 8, 9}; int n = 6; int x = 7; int floor = -1, ceil = -1; for (int i = 0; i < n; i++) { if (arr[i] <= x) floor = arr[i]; if (arr[i] >= x && ceil == -1) ceil = arr[i]; } printf("Floor: %d, Ceil: %d\n", floor, ceil);
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
The floor of 7 is 6 because 6 is the greatest number less than or equal to 7 in the array. The ceil of 7 is 8 because 8 is the smallest number greater than or equal to 7.
❓ Predict Output
intermediateOutput when value is smaller than all elements
What is the output of the code when searching floor and ceil of 0 in the sorted array?
DSA C
int arr[] = {2, 4, 6, 8, 10}; int n = 5; int x = 0; int floor = -1, ceil = -1; for (int i = 0; i < n; i++) { if (arr[i] <= x) floor = arr[i]; if (arr[i] >= x && ceil == -1) ceil = arr[i]; } printf("Floor: %d, Ceil: %d\n", floor, ceil);
Attempts:
2 left
💡 Hint
Floor is -1 if no element is less than or equal to x.
✗ Incorrect
Since 0 is smaller than all elements, floor remains -1 (no element <= 0), ceil is the smallest element 2.
❓ Predict Output
advancedOutput when value is larger than all elements
What is the output of the code when searching floor and ceil of 15 in the sorted array?
DSA C
int arr[] = {3, 5, 7, 10, 12}; int n = 5; int x = 15; int floor = -1, ceil = -1; for (int i = 0; i < n; i++) { if (arr[i] <= x) floor = arr[i]; if (arr[i] >= x && ceil == -1) ceil = arr[i]; } printf("Floor: %d, Ceil: %d\n", floor, ceil);
Attempts:
2 left
💡 Hint
Ceil is -1 if no element is greater than or equal to x.
✗ Incorrect
Since 15 is larger than all elements, floor is the largest element 12, ceil remains -1 (no element >= 15).
🧠 Conceptual
advancedUnderstanding floor and ceil definitions
Which of the following statements correctly defines floor and ceil in a sorted array?
Attempts:
2 left
💡 Hint
Floor is always less than or equal to x, ceil is always greater than or equal to x.
✗ Incorrect
Floor is the greatest element that does not exceed x, ceil is the smallest element that is not less than x.
❓ Predict Output
expertOutput of binary search based floor and ceil finder
What is the output of the following code that uses binary search to find floor and ceil of 4 in the sorted array?
DSA C
int arr[] = {1, 2, 4, 6, 8}; int n = 5; int x = 4; int floor = -1, ceil = -1; int low = 0, high = n - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == x) { floor = arr[mid]; ceil = arr[mid]; break; } else if (arr[mid] < x) { floor = arr[mid]; low = mid + 1; } else { ceil = arr[mid]; high = mid - 1; } } printf("Floor: %d, Ceil: %d\n", floor, ceil);
Attempts:
2 left
💡 Hint
If the element is found, floor and ceil are both that element.
✗ Incorrect
Since 4 is present in the array, both floor and ceil are 4.
