The floor of a number is the greatest element in the array that is less than or equal to that number.
Click to reveal answer
beginner
What is the ceil of a number in a sorted array?
The ceil of a number is the smallest element in the array that is greater than or equal to that number.
Click to reveal answer
intermediate
Why is binary search useful for finding floor and ceil in a sorted array?
Binary search helps find floor and ceil efficiently by repeatedly dividing the search space in half, reducing time complexity to O(log n).
Click to reveal answer
intermediate
If the target number is smaller than all elements in the array, what is the floor and ceil?
Floor does not exist (no element ≤ target), ceil is the smallest element in the array.
Click to reveal answer
intermediate
If the target number is larger than all elements in the array, what is the floor and ceil?
Floor is the largest element in the array, ceil does not exist (no element ≥ target).
Click to reveal answer
What does the floor of a number represent in a sorted array?
AThe greatest element less than or equal to the number
BThe first element in the array
CThe average of all elements less than the number
DThe smallest element greater than or equal to the number
✗ Incorrect
Floor is the greatest element less than or equal to the target number.
What is the time complexity of finding floor or ceil using binary search in a sorted array?
AO(log n)
BO(n log n)
CO(n)
DO(1)
✗ Incorrect
Binary search reduces the search space by half each step, resulting in O(log n) time.
If the target number is exactly equal to an element in the array, what are floor and ceil?
AFloor is smaller element, ceil is larger element
BBoth floor and ceil are that element
CFloor and ceil do not exist
DFloor is the first element, ceil is the last element
✗ Incorrect
When the target matches an element, floor and ceil are that element itself.
If the target number is smaller than all elements in the array, what is the floor?
AThe smallest element
BThe largest element
CNo floor exists
DThe middle element
✗ Incorrect
No element is less than or equal to the target, so floor does not exist.
Which search method is best to find floor and ceil in a sorted array?
ALinear search
BBubble sort
CDepth-first search
DBinary search
✗ Incorrect
Binary search efficiently finds floor and ceil in O(log n) time.
Explain how to find the floor and ceil of a number in a sorted array using binary search.
Think about how to move left or right in the array to find closest smaller or larger elements.
You got /4 concepts.
Describe what happens when the target number is outside the range of the sorted array elements for floor and ceil.
Consider the smallest and largest elements in the array.
You got /3 concepts.
Practice
(1/5)
1. What does the floor of a number mean in a sorted array context?
easy
A. The greatest element less than or equal to the number
B. The smallest element greater than the number
C. The average of all elements less than the number
D. The smallest element less than the number
Solution
Step 1: Understand floor definition in sorted array
The floor of a number is the greatest element in the array that is less than or equal to the number.
Step 2: Compare options with definition
The greatest element less than or equal to the number matches the definition exactly, others describe different concepts.
Final Answer:
The greatest element less than or equal to the number -> Option A
Quick Check:
Floor = greatest ≤ number [OK]
Hint: Floor is always ≤ target, pick greatest such element [OK]
Common Mistakes:
Confusing floor with ceil
Thinking floor is strictly less than the number
Mixing floor with average or median
2. Which of the following is the correct Java method signature to find the floor of a number in a sorted integer array?
easy
A. int findFloor(int arr[], target)
B. public int findFloor(int[] arr, int target, int size)
C. static int findFloor(int[] arr, int target) throws Exception
D. public static int findFloor(int[] arr, int target)
Solution
Step 1: Check Java method signature syntax
Java methods must specify access modifier, return type, method name, and parameter types.
Step 2: Validate each option
public static int findFloor(int[] arr, int target) is correct: public static int findFloor(int[] arr, int target). int findFloor(int arr[], target) misses access modifier and parameter type for target. static int findFloor(int[] arr, int target) throws Exception unnecessarily throws Exception. public int findFloor(int[] arr, int target, int size) has extra parameter 'size' not needed.
Final Answer:
public static int findFloor(int[] arr, int target) -> Option D
Quick Check:
Correct Java method signature = public static int findFloor(int[] arr, int target) [OK]
4. Consider this Java method to find floor in a sorted array:
public static int findFloor(int[] arr, int target) {
int low = 0, high = arr.length - 1;
int floor = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) return arr[mid];
else if (arr[mid] < target) {
floor = arr[mid];
low = mid + 1;
} else {
high = mid - 1;
}
}
return floor;
}
What is the bug if the array is empty?
medium
A. Returns -1 correctly as floor does not exist
B. Throws ArrayIndexOutOfBoundsException at mid calculation
C. Returns 0 incorrectly as floor
D. Infinite loop occurs
Solution
Step 1: Analyze behavior with empty array
If array is empty, arr.length = 0, so high = -1. Condition low <= high is false initially, so loop never runs.
Step 2: Check return value when loop doesn't run
Variable floor initialized to -1 and returned as is, indicating no floor found.
Final Answer:
Returns -1 correctly as floor does not exist -> Option A
Quick Check:
Empty array returns -1 floor [OK]
Hint: Check loop condition before accessing array [OK]
Common Mistakes:
Assuming mid calculation causes error on empty array
Expecting exception instead of -1 return
Missing initialization of floor variable
5. You have a sorted array int[] arr = {2, 4, 6, 8, 10}. You want to find the floor and ceil for target = 1 and target = 11. What are the correct floor and ceil values for these targets?
hard
A. For 1: Floor = -1, Ceil = 2; For 11: Floor = -1, Ceil = -1
B. For 1: Floor = -1, Ceil = 2; For 11: Floor = 10, Ceil = -1
C. For 1: Floor = -1, Ceil = -1; For 11: Floor = 10, Ceil = 10
D. For 1: Floor = 2, Ceil = -1; For 11: Floor = -1, Ceil = 10
Solution
Step 1: Find floor and ceil for target = 1
Floor is greatest element ≤ 1. No elements ≤ 1, so floor = -1 (not found). Ceil is smallest element ≥ 1, which is 2.
Step 2: Find floor and ceil for target = 11
Floor is greatest element ≤ 11, which is 10. Ceil is smallest element ≥ 11, none exists, so ceil = -1.
Final Answer:
For 1: Floor = -1, Ceil = 2; For 11: Floor = 10, Ceil = -1 -> Option B
Quick Check:
Floor and ceil outside array bounds handled with -1 [OK]
Hint: Floor or ceil missing means return -1 for that value [OK]
Common Mistakes:
Returning array ends incorrectly as floor or ceil
Confusing floor and ceil for out-of-bound targets
Assuming floor or ceil always exists
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.