Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Floor and Ceil Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Find floor and ceil for a value in a sorted array
What is the output of the following code that finds the floor and ceil of 7 in the sorted array?
DSA C
int arr[] = {1, 3, 5, 6, 8, 9};
int n = 6;
int x = 7;
int floor = -1, ceil = -1;
for (int i = 0; i < n; i++) {
    if (arr[i] <= x) floor = arr[i];
    if (arr[i] >= x && ceil == -1) ceil = arr[i];
}
printf("Floor: %d, Ceil: %d\n", floor, ceil);
AFloor: 8, Ceil: 9
BFloor: 5, Ceil: 6
CFloor: 7, Ceil: 7
DFloor: 6, Ceil: 8
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.
❓ Predict Output
intermediate
2:00remaining
Output when value is smaller than all elements
What is the output of the code when searching floor and ceil of 0 in the sorted array?
DSA C
int arr[] = {2, 4, 6, 8, 10};
int n = 5;
int x = 0;
int floor = -1, ceil = -1;
for (int i = 0; i < n; i++) {
    if (arr[i] <= x) floor = arr[i];
    if (arr[i] >= x && ceil == -1) ceil = arr[i];
}
printf("Floor: %d, Ceil: %d\n", floor, ceil);
AFloor: 2, Ceil: 2
BFloor: -1, Ceil: 2
CFloor: -1, Ceil: -1
DFloor: 0, Ceil: 2
Attempts:
2 left
💡 Hint
Floor is -1 if no element is less than or equal to x.
❓ Predict Output
advanced
2:00remaining
Output when value is larger than all elements
What is the output of the code when searching floor and ceil of 15 in the sorted array?
DSA C
int arr[] = {3, 5, 7, 10, 12};
int n = 5;
int x = 15;
int floor = -1, ceil = -1;
for (int i = 0; i < n; i++) {
    if (arr[i] <= x) floor = arr[i];
    if (arr[i] >= x && ceil == -1) ceil = arr[i];
}
printf("Floor: %d, Ceil: %d\n", floor, ceil);
AFloor: 12, Ceil: -1
BFloor: -1, Ceil: 15
CFloor: 15, Ceil: 15
DFloor: 10, Ceil: 12
Attempts:
2 left
💡 Hint
Ceil is -1 if no element is greater than or equal to x.
🧠 Conceptual
advanced
1:30remaining
Understanding floor and ceil definitions
Which of the following statements correctly defines floor and ceil in a sorted array?
AFloor is the smallest element less than x; Ceil is the greatest element greater than x.
BFloor is the smallest element greater than or equal to x; Ceil is the greatest element less than or equal to x.
CFloor is the greatest element less than or equal to x; Ceil is the smallest element greater than or equal to x.
DFloor and Ceil both are the closest elements to x regardless of being smaller or greater.
Attempts:
2 left
💡 Hint
Floor is always less than or equal to x, ceil is always greater than or equal to x.
❓ Predict Output
expert
2:30remaining
Output of binary search based floor and ceil finder
What is the output of the following code that uses binary search to find floor and ceil of 4 in the sorted array?
DSA C
int arr[] = {1, 2, 4, 6, 8};
int n = 5;
int x = 4;
int floor = -1, ceil = -1;
int low = 0, high = n - 1;
while (low <= high) {
    int mid = low + (high - low) / 2;
    if (arr[mid] == x) {
        floor = arr[mid];
        ceil = arr[mid];
        break;
    } else if (arr[mid] < x) {
        floor = arr[mid];
        low = mid + 1;
    } else {
        ceil = arr[mid];
        high = mid - 1;
    }
}
printf("Floor: %d, Ceil: %d\n", floor, ceil);
AFloor: 4, Ceil: 4
BFloor: 2, Ceil: 6
CFloor: 1, Ceil: 8
DFloor: -1, Ceil: -1
Attempts:
2 left
💡 Hint
If the element is found, floor and ceil are both that element.

Practice

(1/5)
1. What is the floor of a number in a sorted array?
easy
A. The smallest element strictly less than the number
B. The smallest element greater than or equal to the number
C. The average of all elements less than the number
D. The greatest element less than or equal to 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 given number.
  2. Step 2: Compare options with definition

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

    The greatest element less than or equal to the number -> Option D
  4. Quick Check:

    Floor = greatest ≤ number [OK]
Hint: Floor is always ≤ number, pick greatest such element [OK]
Common Mistakes:
  • Confusing floor with ceil
  • Thinking floor is strictly less than number
  • Mixing floor with average or median
2. Which of the following is the correct C syntax to find the middle index in binary search for array of size n?
easy
A. int mid = (0 + n) / 2;
B. int mid = 0 + n - 1 / 2;
C. int mid = (0 + (n - 1)) >> 1;
D. int mid = n / 2;

Solution

  1. Step 1: Understand middle index calculation in binary search

    For array indices 0 to n-1, middle is (0 + n-1) / 2 or equivalent.
  2. Step 2: Analyze options for correct syntax and efficiency

    int mid = (0 + (n - 1)) >> 1; uses bitwise right shift for division by 2, which is efficient and correct for integer mid calculation.
  3. Final Answer:

    int mid = (0 + (n - 1)) >> 1; -> Option C
  4. Quick Check:

    Mid = (start + end) >> 1 [OK]
Hint: Use (start + end) >> 1 for efficient mid calculation [OK]
Common Mistakes:
  • Using n instead of n-1 as end index
  • Forgetting parentheses causing wrong precedence
  • Using floating division instead of integer
3. Given the sorted array arr = {1, 3, 5, 7, 9}, what is the floor of 6?
medium
A. 7
B. 5
C. 6
D. -1

Solution

  1. Step 1: Identify elements less than or equal to 6

    Elements ≤ 6 are 1, 3, 5.
  2. Step 2: Find greatest among these

    The greatest element ≤ 6 is 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Floor(6) = 5 [OK]
Hint: Floor is largest ≤ target in sorted array [OK]
Common Mistakes:
  • Choosing 7 which is greater than 6
  • Assuming floor equals the number if not present
  • Returning -1 incorrectly when floor exists
4. Consider this code snippet to find ceil in a sorted array int ceil = -1; for(int i=0; i= x) { ceil = arr[i]; break; } }. What is the main issue?
medium
A. It may return wrong value if array is not sorted
B. It does not handle empty array
C. It returns first element ≥ x, which is correct for ceil
D. It finds floor instead of ceil

Solution

  1. Step 1: Understand the code logic

    The code returns the first element in array ≥ x, which is correct if array is sorted.
  2. Step 2: Identify assumptions and issues

    If array is not sorted, first element ≥ x may not be the smallest such element, so ceil is incorrect.
  3. Final Answer:

    It may return wrong value if array is not sorted -> Option A
  4. Quick Check:

    Ceil requires sorted array for correctness [OK]
Hint: Ceil logic needs sorted array to work correctly [OK]
Common Mistakes:
  • Ignoring array sorting requirement
  • Confusing ceil with floor
  • Not checking for empty array before loop
5. You have a sorted array arr = {2, 4, 6, 8, 10}. If you want to find the ceil of 11 using binary search, what should be the returned value?
hard
A. -1
B. 11
C. 10
D. Error

Solution

  1. Step 1: Understand ceil definition and array elements

    Ceil is smallest element ≥ target. Here, target is 11, which is greater than all elements.
  2. Step 2: Check if any element ≥ 11 exists

    No element in array ≥ 11, so ceil does not exist, return -1.
  3. Final Answer:

    -1 -> Option A
  4. Quick Check:

    No element ≥ 11 means ceil = -1 [OK]
Hint: If target > max element, ceil is -1 (not found) [OK]
Common Mistakes:
  • Returning last element as ceil incorrectly
  • Returning target itself if not found
  • Not handling no ceil case properly
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