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 from linear to logarithmic.
Click to reveal answer
beginner
In a sorted array [1, 3, 5, 7], what is the floor and ceil of 4?
Floor is 3 (largest ≤ 4), Ceil is 5 (smallest ≥ 4).
Click to reveal answer
intermediate
What happens if the floor or ceil does not exist in the array?
If floor does not exist, it means all elements are greater than the number; if ceil does not exist, all elements are smaller. Usually, we return -1 or null.
Click to reveal answer
What does the floor of a number represent in a sorted array?
AGreatest element greater than the number
BSmallest element greater than or equal to the number
CGreatest element less than or equal to the number
DSmallest element less than the number
✗ Incorrect
Floor is the greatest element that is 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(n)
BO(1)
CO(n log n)
DO(log n)
✗ Incorrect
Binary search divides the search space by half each time, so it runs in O(log n) time.
If the number is smaller than all elements in the array, what is the floor?
ADoes not exist (e.g., -1)
BThe largest element
CThe smallest element
DThe number itself
✗ Incorrect
No element is less than or equal to the number, so floor does not exist.
In the array [2, 4, 6, 8], what is the ceil of 5?
A6
B5
C4
D8
✗ Incorrect
Ceil is the smallest element greater than or equal to 5, which is 6.
Which search method is best to find floor and ceil in a sorted array?
ALinear search
BBinary search
CBubble sort
DInsertion sort
✗ 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 binary search narrows down the position and how to track candidates.
You got /4 concepts.
Describe what happens when the target number is smaller than all elements or greater than all elements in the array when finding floor and ceil.
Consider the array boundaries and what floor and ceil mean.
You got /3 concepts.
Practice
(1/5)
1. What does the floor of a number mean in a sorted array context?
easy
A. The average of all elements less than the number
B. The smallest element greater than or equal to the number
C. The smallest element strictly less than the number
D. The greatest element less than or equal to 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 that number.
Step 2: Compare options with definition
The greatest element less than or equal to the number matches the definition exactly. Options A, B, and D do not match the floor concept.
Final Answer:
The greatest element less than or equal to the number -> Option D
Quick Check:
Floor = greatest ≤ number [OK]
Hint: Floor is always ≤ the target number [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 C# method signatures correctly represents a function to find the floor in a sorted integer array (with array as first parameter)?
easy
A. void FindFloor(int[] arr, int target)
B. int FindFloor(int[] arr, int target)
C. int FindFloor(int target, int[] arr)
D. int FindFloor(List<int> arr, string target)
Solution
Step 1: Check method return type and parameters
The function should return an integer (the floor value) and accept an integer array and an integer target.
Step 2: Validate each option
int FindFloor(int[] arr, int target) matches the correct signature: returns int, takes int[] and int. void FindFloor(int[] arr, int target) returns void, so no result. int FindFloor(int target, int[] arr) swaps parameters order (less common but possible). int FindFloor(List<int> arr, string target) uses wrong types (List<int> and string target).
Final Answer:
int FindFloor(int[] arr, int target) -> Option B
Quick Check:
Return int, params (int[], int) [OK]
Hint: Floor function returns int and takes int array, int target [OK]
Common Mistakes:
Using void return type
Swapping parameter order incorrectly
Using wrong parameter types
3. What is the output of the following C# code?
int[] arr = {1, 3, 5, 7, 9};
int target = 6;
int floor = -1;
int left = 0, right = arr.Length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
floor = arr[mid];
break;
} else if (arr[mid] < target) {
floor = arr[mid];
left = mid + 1;
} else {
right = mid - 1;
}
}
Console.WriteLine(floor);
Hint: Floor is last smaller or equal element found in binary search [OK]
Common Mistakes:
Returning 7 instead of 5
Confusing floor with ceil
Not updating floor inside loop
4. The following code is intended to find the ceil of a target in a sorted array but has a bug. What is the bug?
int Ceil(int[] arr, int target) {
int left = 0, right = arr.Length - 1;
int ceil = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return arr[mid];
} else if (arr[mid] > target) {
ceil = arr[mid];
right = mid + 1;
} else {
left = mid + 1;
}
}
return ceil;
}
medium
A. The function should return -1 if no ceil found
B. left should be updated to mid - 1, not mid + 1
C. right should be updated to mid - 1, not mid + 1
D. ceil should be initialized to 0, not -1
Solution
Step 1: Analyze right pointer update in binary search
When arr[mid] > target, we want to search left half, so right should move to mid - 1, not mid + 1.
Step 2: Identify impact of wrong update
Using right = mid + 1 moves right pointer incorrectly, causing infinite loop or wrong result.
Final Answer:
right should be updated to mid - 1, not mid + 1 -> Option C
Quick Check:
Correct binary search halves pointers [OK]
Hint: When arr[mid] > target, move right to mid - 1 [OK]
Common Mistakes:
Updating right pointer incorrectly
Confusing ceil with floor logic
Not returning correct default value
5. Given a sorted array arr = {2, 4, 6, 8, 10} and a target 5, write a C# method to return both floor and ceil values as a tuple. What should the method return?
medium
A. (4, 6)
B. (2, 4)
C. (5, 5)
D. (-1, -1)
Solution
Step 1: Identify floor and ceil for target 5 in array
Array: [2,4,6,8,10]. Floor is greatest ≤ 5, which is 4. Ceil is smallest ≥ 5, which is 6.