Challenge - 5 Problems
FloorCeilMaster
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of floor and ceil search for a value in a sorted array
What is the output of the following Java code that finds floor and ceil of 7 in a sorted array?
DSA Java
public class FloorCeil { public static int floor(int[] arr, int x) { int low = 0, high = arr.length - 1, res = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == x) return arr[mid]; if (arr[mid] < x) { res = arr[mid]; low = mid + 1; } else { high = mid - 1; } } return res; } public static int ceil(int[] arr, int x) { int low = 0, high = arr.length - 1, res = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == x) return arr[mid]; if (arr[mid] > x) { res = arr[mid]; high = mid - 1; } else { low = mid + 1; } } return res; } public static void main(String[] args) { int[] arr = {1, 3, 5, 6, 8, 9}; int x = 7; System.out.println("Floor: " + floor(arr, x)); System.out.println("Ceil: " + ceil(arr, x)); } }
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 in the array is 6 (largest number less than 7), and the ceil is 8 (smallest number greater than 7).
🧠 Conceptual
intermediateUnderstanding floor and ceil in sorted arrays
In a sorted array, what does the 'floor' of a value x represent?
Attempts:
2 left
💡 Hint
Floor is about the closest smaller or equal value to x.
✗ Incorrect
Floor of x is the greatest element in the array that is less than or equal to x.
🔧 Debug
advancedIdentify the error in floor function implementation
What error will occur when running this floor function on array {2,4,6,8} with x=1?
DSA Java
public static int floor(int[] arr, int x) { int low = 0, high = arr.length - 1, res = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == x) return arr[mid]; if (arr[mid] < x) { res = arr[mid]; low = mid + 1; } else { high = mid - 1; } } return res; }
Attempts:
2 left
💡 Hint
Check what happens if x is smaller than all elements.
✗ Incorrect
Since 1 is smaller than all elements, no floor exists, so function returns -1.
❓ Predict Output
advancedOutput of floor and ceil for value present in array
What is the output of the following code for x=5 in array {1,3,5,7,9}?
DSA Java
public class FloorCeil { public static int floor(int[] arr, int x) { int low = 0, high = arr.length - 1, res = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == x) return arr[mid]; if (arr[mid] < x) { res = arr[mid]; low = mid + 1; } else { high = mid - 1; } } return res; } public static int ceil(int[] arr, int x) { int low = 0, high = arr.length - 1, res = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == x) return arr[mid]; if (arr[mid] > x) { res = arr[mid]; high = mid - 1; } else { low = mid + 1; } } return res; } public static void main(String[] args) { int[] arr = {1, 3, 5, 7, 9}; int x = 5; System.out.println("Floor: " + floor(arr, x)); System.out.println("Ceil: " + ceil(arr, x)); } }
Attempts:
2 left
💡 Hint
If x is present, floor and ceil should be x itself.
✗ Incorrect
Since 5 is in the array, both floor and ceil return 5.
🚀 Application
expertFind number of elements within a range using floor and ceil
Given a sorted array, how can you find the count of elements between two values low and high (inclusive) using floor and ceil functions?
Attempts:
2 left
💡 Hint
Think about the positions of floor and ceil relative to the range boundaries.
✗ Incorrect
The count of elements in [low, high] is the difference between indices of floor(high) and ceil(low) plus one.
