📖 Scenario: You are working on a program that helps users find the closest values in a sorted list of numbers. This is useful in many real-life situations, like finding the nearest available price or the closest date before or after a given day.
🎯 Goal: Build a program that finds the floor and ceil values for a given number in a sorted array. The floor is the greatest number less than or equal to the target, and the ceil is the smallest number greater than or equal to the target.
📋 What You'll Learn
Create a sorted integer array with specific values
Create a target integer variable
Write code to find the floor and ceil values for the target in the array
Print the floor and ceil values
💡 Why This Matters
🌍 Real World
Finding floor and ceil values is useful in pricing systems, date pickers, and recommendation engines where you need the closest available option.
💼 Career
Understanding floor and ceil concepts helps in software development roles that involve searching, sorting, and data analysis.
Progress0 / 4 steps
1
Create the sorted array
Create a sorted integer array called numbers with these exact values: 2, 4, 6, 8, 10, 12, 14.
DSA C#
Hint
Use an integer array with the exact values in ascending order.
2
Set the target number
Create an integer variable called target and set it to 9.
DSA C#
Hint
Use a simple integer variable to hold the target value.
3
Find floor and ceil values
Write code to find the floor and ceil values of target in the numbers array. Use two integer variables called floor and ceil. Initialize floor to -1 and ceil to -1. Use a for loop with variable i to check each number in numbers. Update floor to the greatest number less than or equal to target. Update ceil to the smallest number greater than or equal to target. Assume the array is sorted in ascending order.
DSA C#
Hint
Loop through the array and update floor and ceil based on comparisons with target.
4
Print the floor and ceil values
Print the floor and ceil values using Console.WriteLine in this exact format: Floor: {floor} and Ceil: {ceil}.
DSA C#
Hint
Use Console.WriteLine with string interpolation to print floor and ceil.
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.