0
0
DSA C++programming~20 mins

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

Choose your learning style9 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
Output of floor and ceil search in sorted array
What is the output of the following C++ code that finds floor and ceil of a target in a sorted array?
DSA C++
int floorIndex(const vector<int>& arr, int target) {
    int left = 0, right = arr.size() - 1, floor = -1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) return mid;
        else if (arr[mid] < target) {
            floor = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return floor;
}

int ceilIndex(const vector<int>& arr, int target) {
    int left = 0, right = arr.size() - 1, ceil = -1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) return mid;
        else if (arr[mid] > target) {
            ceil = mid;
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return ceil;
}

int main() {
    vector<int> arr = {1, 3, 5, 7, 9};
    int target = 6;
    int f = floorIndex(arr, target);
    int c = ceilIndex(arr, target);
    cout << "Floor index: " << f << ", Ceil index: " << c << endl;
    return 0;
}
AFloor index: 3, Ceil index: 2
BFloor index: 1, Ceil index: 4
CFloor index: 2, Ceil index: 3
DFloor index: -1, Ceil index: -1
Attempts:
2 left
💡 Hint
Remember floor is the greatest element less than or equal to target, ceil is the smallest element greater than or equal to target.
🧠 Conceptual
intermediate
1:00remaining
Understanding floor and ceil in sorted arrays
In a sorted array, what does the floor of a target value represent?
AThe element exactly equal to the target if it exists, otherwise -1
BThe greatest element less than or equal to the target
CThe smallest element greater than or equal to the target
DThe average of the smallest and greatest elements in the array
Attempts:
2 left
💡 Hint
Floor is about the closest smaller or equal value to the target.
🔧 Debug
advanced
1:30remaining
Identify the error in floor function implementation
What error will the following floor function cause when called with target=4 on array {1,3}?
DSA C++
int floorIndex(const vector<int>& arr, int target) {
    int left = 0, right = arr.size() - 1, floor = -1;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) return mid;
        else if (arr[mid] < target) {
            floor = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return floor;
}
AReturns incorrect floor index 0
BThrows runtime error due to invalid index
CRuns into infinite loop
DReturns -1 correctly indicating no floor found
Attempts:
2 left
💡 Hint
Check the loop condition and how left and right pointers move.
Predict Output
advanced
1:30remaining
Output of ceil function with target present in array
What is the output of the following code snippet?
DSA C++
vector<int> arr = {2, 4, 6, 8, 10};
int target = 8;
int left = 0, right = arr.size() - 1, ceil = -1;
while (left <= right) {
    int mid = left + (right - left) / 2;
    if (arr[mid] == target) {
        ceil = mid;
        break;
    } else if (arr[mid] > target) {
        ceil = mid;
        right = mid - 1;
    } else {
        left = mid + 1;
    }
}
cout << "Ceil index: " << ceil << endl;
ACeil index: 4
BCeil index: 2
CCeil index: -1
DCeil index: 3
Attempts:
2 left
💡 Hint
If target is present, ceil index should be the index of target itself.
🧠 Conceptual
expert
1:00remaining
Floor and Ceil behavior on empty array
What are the floor and ceil indices returned by the standard binary search approach when the input array is empty?
ABoth floor and ceil return -1 indicating no elements
BFloor returns 0, Ceil returns 0
CFloor returns -1, Ceil returns 0
DBoth floor and ceil cause runtime error due to invalid access
Attempts:
2 left
💡 Hint
Think about the initial values and loop conditions when array size is zero.