📖 Scenario: Imagine you have a sorted list of house numbers on a street. You want to find the closest house number that is less than or equal to a visitor's number (floor), and the closest house number that is greater than or equal to the visitor's number (ceil).
🎯 Goal: Build a Java program that finds the floor and ceil values for a given number in a sorted array of integers.
📋 What You'll Learn
Create a sorted integer array named houses with exact values
Create an integer variable named visitorNumber with a specific value
Write a method findFloorAndCeil that takes the array and visitor number and returns floor and ceil
Print the floor and ceil values in the format: Floor: X, Ceil: Y
💡 Why This Matters
🌍 Real World
Finding floor and ceil values is useful in real estate, pricing, and scheduling where you want to find closest matches or limits.
💼 Career
Understanding floor and ceil concepts helps in algorithm design, data searching, and optimization tasks common in software development.
Progress0 / 4 steps
1
Create the sorted array of house numbers
Create a sorted integer array called houses with these exact values: 2, 5, 8, 12, 16, 23, 38, 56, 72, 91
DSA Java
Hint
Use int[] houses = { ... }; to create the array with the exact numbers.
2
Set the visitor's house number
Create an integer variable called visitorNumber and set it to 20
DSA Java
Hint
Use int visitorNumber = 20; to store the visitor's number.
3
Write the method to find floor and ceil
Write a method called findFloorAndCeil that takes int[] houses and int visitorNumber as parameters and returns an integer array of size 2 where the first element is the floor and the second is the ceil. Use a for loop to find the floor and ceil values.
DSA Java
Hint
Use a for-each loop to check each house number. Update floor if house is less than or equal to visitorNumber. Set ceil when you find the first house greater than or equal to visitorNumber.
4
Print the floor and ceil values
Call the method findFloorAndCeil with houses and visitorNumber, store the result in result, and print the floor and ceil values in the format: Floor: X, Ceil: Y
DSA Java
Hint
Call findFloorAndCeil(houses, visitorNumber), save the result, and print using System.out.println.
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.