Bird
Raised Fist0
DSA Javaprogramming~20 mins

Floor and Ceil in Sorted Array in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FloorCeilMaster
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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));
    }
}
A{"Floor":6,"Ceil":8}
B{"Floor":5,"Ceil":6}
C{"Floor":7,"Ceil":7}
D{"Floor":8,"Ceil":9}
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.
🧠 Conceptual
intermediate
1:00remaining
Understanding floor and ceil in sorted arrays
In a sorted array, what does the 'floor' of a value x represent?
AThe smallest element greater than x
BThe average of all elements less than x
CThe exact element x if present, otherwise -1
DThe largest element less than or equal to x
Attempts:
2 left
💡 Hint
Floor is about the closest smaller or equal value to x.
🔧 Debug
advanced
1:30remaining
Identify 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;
}
AThrows ArrayIndexOutOfBoundsException
BReturns -1 indicating no floor found
CReturns 2 incorrectly as floor
DInfinite loop occurs
Attempts:
2 left
💡 Hint
Check what happens if x is smaller than all elements.
❓ Predict Output
advanced
2:00remaining
Output 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));
    }
}
A{"Floor":5,"Ceil":5}
B{"Floor":3,"Ceil":7}
C{"Floor":1,"Ceil":9}
D{"Floor":-1,"Ceil":-1}
Attempts:
2 left
💡 Hint
If x is present, floor and ceil should be x itself.
🚀 Application
expert
2:30remaining
Find 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?
ACount = index of ceil(high) - index of floor(low) + 1
BCount = index of ceil(high) - index of ceil(low) + 1
CCount = index of floor(high) - index of ceil(low) + 1
DCount = index of floor(high) - index of floor(low) + 1
Attempts:
2 left
💡 Hint
Think about the positions of floor and ceil relative to the range boundaries.

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

  1. 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.
  2. Step 2: Compare options with definition

    The greatest element less than or equal to the number matches the definition exactly, others describe different concepts.
  3. Final Answer:

    The greatest element less than or equal to the number -> Option A
  4. 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

  1. Step 1: Check Java method signature syntax

    Java methods must specify access modifier, return type, method name, and parameter types.
  2. 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.
  3. Final Answer:

    public static int findFloor(int[] arr, int target) -> Option D
  4. Quick Check:

    Correct Java method signature = public static int findFloor(int[] arr, int target) [OK]
Hint: Java method needs access modifier, return type, parameters [OK]
Common Mistakes:
  • Omitting access modifier
  • Missing parameter types
  • Adding unnecessary throws clause
3. Given the sorted array int[] arr = {1, 3, 5, 7, 9} and target = 6, what is the floor and ceil of 6?
medium
A. Floor = 7, Ceil = 9
B. Floor = 3, Ceil = 5
C. Floor = 5, Ceil = 7
D. Floor = 6, Ceil = 6

Solution

  1. Step 1: Identify floor of 6 in array

    Floor is greatest element ≤ 6. Elements ≤ 6 are 1, 3, 5. Greatest is 5.
  2. Step 2: Identify ceil of 6 in array

    Ceil is smallest element ≥ 6. Elements ≥ 6 are 7, 9. Smallest is 7.
  3. Final Answer:

    Floor = 5, Ceil = 7 -> Option C
  4. Quick Check:

    Floor ≤ 6 = 5, Ceil ≥ 6 = 7 [OK]
Hint: Floor ≤ target, ceil ≥ target; pick closest values [OK]
Common Mistakes:
  • Choosing floor greater than target
  • Choosing ceil less than target
  • Assuming target must be in array
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

  1. 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.
  2. Step 2: Check return value when loop doesn't run

    Variable floor initialized to -1 and returned as is, indicating no floor found.
  3. Final Answer:

    Returns -1 correctly as floor does not exist -> Option A
  4. 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

  1. 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.
  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.
  3. Final Answer:

    For 1: Floor = -1, Ceil = 2; For 11: Floor = 10, Ceil = -1 -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard