0
0
DSA C++programming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Floor and Ceil Mastery
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 BST
What is the output of the following code that finds floor and ceil of a key in a BST?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

int floorBST(Node* root, int key) {
    int floor = -1;
    while (root) {
        if (root->val == key) return root->val;
        if (root->val > key) root = root->left;
        else {
            floor = root->val;
            root = root->right;
        }
    }
    return floor;
}

int ceilBST(Node* root, int key) {
    int ceil = -1;
    while (root) {
        if (root->val == key) return root->val;
        if (root->val < key) root = root->right;
        else {
            ceil = root->val;
            root = root->left;
        }
    }
    return ceil;
}

int main() {
    Node* root = new Node(8);
    root->left = new Node(4);
    root->right = new Node(12);
    root->left->left = new Node(2);
    root->left->right = new Node(6);
    root->right->left = new Node(10);
    root->right->right = new Node(14);

    int key = 5;
    int f = floorBST(root, key);
    int c = ceilBST(root, key);
    std::cout << "Floor: " << f << ", Ceil: " << c << std::endl;
    return 0;
}
AFloor: -1, Ceil: -1
BFloor: 6, Ceil: 4
CFloor: 5, Ceil: 5
DFloor: 4, Ceil: 6
Attempts:
2 left
💡 Hint
Remember floor is the greatest value less than or equal to key, ceil is the smallest value greater than or equal to key.
🧠 Conceptual
intermediate
1:30remaining
Understanding Floor and Ceil in BST
In a Binary Search Tree (BST), which of the following statements about floor and ceil of a key is always true?
AFloor and Ceil are always equal to the key if the key exists in BST; otherwise both are -1.
BFloor is the smallest value in BST greater than the key; Ceil is the largest value in BST less than the key.
CFloor is the largest value in BST less than or equal to the key; Ceil is the smallest value in BST greater than or equal to the key.
DFloor is always the root value; Ceil is always the leaf value.
Attempts:
2 left
💡 Hint
Think about the definitions of floor and ceil in number sets.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Floor Function
What error or wrong output will the following floor function produce when searching for key=1 in the BST below? BST structure: 5 / \ 3 7 / \ 2 4 Code: int floorBST(Node* root, int key) { int floor = -1; while (root) { if (root->val == key) return root->val; if (root->val < key) { floor = root->val; root = root->right; } else { root = root->left; } } return floor; }
AReturns 2 (incorrect, 2 is greater than 1)
BReturns -1 (correct floor since 1 is less than all nodes)
CInfinite loop due to wrong traversal
DReturns 3 (incorrect floor)
Attempts:
2 left
💡 Hint
Check the condition that updates floor and the direction of traversal.
Predict Output
advanced
2:00remaining
Output of Floor and Ceil for Key Not in BST
Given the BST below and key=13, what is the output of floor and ceil functions? BST: 10 / \ 5 15 / \ \ 3 7 18 Code snippet: int floorBST(Node* root, int key) { int floor = -1; while (root) { if (root->val == key) return root->val; if (root->val > key) root = root->left; else { floor = root->val; root = root->right; } } return floor; } int ceilBST(Node* root, int key) { int ceil = -1; while (root) { if (root->val == key) return root->val; if (root->val < key) root = root->right; else { ceil = root->val; root = root->left; } } return ceil; } int main() { // BST construction omitted for brevity int key = 13; int f = floorBST(root, key); int c = ceilBST(root, key); std::cout << "Floor: " << f << ", Ceil: " << c << std::endl; return 0; }
AFloor: 7, Ceil: 10
BFloor: 10, Ceil: 15
CFloor: -1, Ceil: -1
DFloor: 15, Ceil: 18
Attempts:
2 left
💡 Hint
Floor is the greatest value less than or equal to 13; ceil is the smallest value greater than or equal to 13.
🚀 Application
expert
2:30remaining
Number of Nodes Between Floor and Ceil in BST
Given a BST and a key, how many nodes have values between the floor and ceil of the key (inclusive)? BST: 20 / \ 10 30 / \ / \ 5 15 25 35 Key = 17 What is the count of nodes with values between floor and ceil of 17 (inclusive)?
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
Find floor and ceil first, then count nodes in that range.