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 7 in a sorted array
What is the output of the following code that finds floor and ceil of 7 in the array?
DSA Python
def floor_ceil(arr, x): floor = -1 ceil = -1 start, end = 0, len(arr) - 1 while start <= end: mid = (start + end) // 2 if arr[mid] == x: return arr[mid], arr[mid] elif arr[mid] < x: floor = arr[mid] start = mid + 1 else: ceil = arr[mid] end = mid - 1 return floor, ceil arr = [1, 3, 5, 6, 8, 9] x = 7 print(floor_ceil(arr, x))
Attempts:
2 left
💡 Hint
Think about how binary search narrows down the floor and ceil values.
✗ Incorrect
The floor of 7 is the greatest element less than or equal to 7, which is 6. The ceil is the smallest element greater than or equal to 7, which is 8.
❓ Predict Output
intermediateOutput when element is smaller than all array elements
What is the output of the code when searching floor and ceil of 0 in the array?
DSA Python
def floor_ceil(arr, x): floor = -1 ceil = -1 start, end = 0, len(arr) - 1 while start <= end: mid = (start + end) // 2 if arr[mid] == x: return arr[mid], arr[mid] elif arr[mid] < x: floor = arr[mid] start = mid + 1 else: ceil = arr[mid] end = mid - 1 return floor, ceil arr = [2, 4, 6, 8, 10] x = 0 print(floor_ceil(arr, x))
Attempts:
2 left
💡 Hint
If x is smaller than all elements, floor should be -1.
✗ Incorrect
Since 0 is smaller than all elements, floor remains -1 (no smaller element), ceil is the smallest element 2.
🔧 Debug
advancedIdentify the error in floor and ceil function
What error does the following code produce when run?
DSA Python
def floor_ceil(arr, x): floor = None ceil = None start, end = 0, len(arr) - 1 while start <= end: mid = (start + end) // 2 if arr[mid] == x: return arr[mid], arr[mid] elif arr[mid] < x: floor = arr[mid] start = mid + 1 else: ceil = arr[mid] end = mid - 1 return floor, ceil arr = [] x = 5 print(floor_ceil(arr, x))
Attempts:
2 left
💡 Hint
Consider what happens when the array is empty and the function returns floor and ceil.
✗ Incorrect
The function returns (None, None) because floor and ceil are initialized as None and no updates happen.
🧠 Conceptual
advancedFloor and Ceil when element is present in array
If the element x is present in the sorted array, what will be the floor and ceil values returned by the function?
Attempts:
2 left
💡 Hint
Check the condition when arr[mid] == x in the code.
✗ Incorrect
When x is found, the function returns (x, x) immediately as floor and ceil.
🚀 Application
expertNumber of elements between floor and ceil
Given a sorted array and a number x, how many elements lie strictly between the floor and ceil of x?
DSA Python
def count_between_floor_ceil(arr, x): floor = -1 ceil = -1 start, end = 0, len(arr) - 1 while start <= end: mid = (start + end) // 2 if arr[mid] == x: floor = ceil = arr[mid] break elif arr[mid] < x: floor = arr[mid] start = mid + 1 else: ceil = arr[mid] end = mid - 1 if floor == -1 or ceil == -1 or floor == ceil: return 0 floor_index = arr.index(floor) ceil_index = arr.index(ceil) return max(0, ceil_index - floor_index - 1) arr = [1, 3, 5, 7, 9, 11] x = 6 print(count_between_floor_ceil(arr, x))
Attempts:
2 left
💡 Hint
Find the indices of floor and ceil and count elements between them.
✗ Incorrect
Floor is 5 at index 2, ceil is 7 at index 3, so elements strictly between are zero because indices are adjacent.
