What is the floor of a value in a Binary Search Tree (BST)?
The floor of a value in a BST is the greatest value in the BST that is less than or equal to the given value.
Click to reveal answer
beginner
What is the ceil of a value in a Binary Search Tree (BST)?
The ceil of a value in a BST is the smallest value in the BST that is greater than or equal to the given value.
Click to reveal answer
intermediate
How does the BST property help in finding floor and ceil efficiently?
Because BST nodes are ordered, we can decide to go left or right at each node to find floor or ceil without checking all nodes, making the search faster.
Click to reveal answer
beginner
In a BST, if the current node's value is equal to the target value, what are the floor and ceil?
Both floor and ceil are the current node's value because it exactly matches the target.
Click to reveal answer
intermediate
If the target value is smaller than the smallest value in the BST, what is the floor and ceil?
Floor does not exist (no value less than or equal), ceil is the smallest value in the BST.
Click to reveal answer
What does the floor of a value in a BST represent?
AThe average of all values in the BST
BThe smallest value greater than or equal to the target
CThe root value of the BST
DThe greatest value less than or equal to the target
✗ Incorrect
Floor is the greatest value in the BST that is less than or equal to the target value.
If the target value equals a node's value in BST, what is the ceil?
AThe node's value itself
BA value smaller than the target
CThe largest value in the BST
DNo ceil exists
✗ Incorrect
When the target equals a node's value, the ceil is that node's value.
Which direction do you move in BST if current node's value is greater than target when finding floor?
ALeft subtree
BStay at current node
CRight subtree
DNo movement needed
✗ Incorrect
If current node's value is greater than target, floor must be in left subtree or not exist.
What is the time complexity to find floor or ceil in a balanced BST?
AO(n log n)
BO(n)
CO(log n)
DO(1)
✗ Incorrect
In a balanced BST, searching floor or ceil takes O(log n) time due to tree height.
If target is smaller than all BST values, what is the floor?
ASmallest BST value
BNo floor exists
CTarget itself
DLargest BST value
✗ Incorrect
No floor exists if target is smaller than all values in BST.
Explain how to find the floor of a given value in a BST step-by-step.
Think about how BST ordering helps decide direction.
You got /6 concepts.
Describe the difference between floor and ceil in a BST with examples.
Use a simple BST and a target value between nodes.
You got /4 concepts.
Practice
(1/5)
1. What does the floor of a value in a Binary Search Tree (BST) represent?
easy
A. The greatest value in the BST less than or equal to the given value
B. The smallest value in the BST greater than or equal to the given value
C. The root value of the BST
D. The average of all values in the BST
Solution
Step 1: Understand the definition of floor in BST
The floor of a value is the greatest value in the BST that is less than or equal to the given value.
Step 2: Compare with other options
The smallest value in the BST greater than or equal to the given value describes ceil, while the remaining options are unrelated to floor concept.
Final Answer:
The greatest value in the BST less than or equal to the given value -> Option A
Quick Check:
Floor = greatest ≤ value [OK]
Hint: Floor is largest value ≤ target in BST [OK]
Common Mistakes:
Confusing floor with ceil
Thinking floor is the smallest value
Assuming floor is always the root
2. Which of the following is the correct C++ function signature to find the ceil of a value in a BST node structure named Node?
easy
A. int ceilBST(int val);
B. void ceilBST(Node root, int val);
C. Node* ceilBST(int val, Node* root);
D. int ceilBST(Node* root, int val);
Solution
Step 1: Analyze function parameters and return type
The function should take a pointer to the root node and the value to find ceil for, returning an integer ceil value.
Step 2: Check each option
int ceilBST(Node* root, int val); matches correct signature: returns int, takes Node* and int. Others have wrong parameter order, missing pointers, or wrong return type.
Final Answer:
int ceilBST(Node* root, int val); -> Option D
Quick Check:
Correct signature returns int and takes Node* root [OK]
Hint: Function needs root pointer and value, returns int [OK]
Common Mistakes:
Passing Node by value instead of pointer
Wrong parameter order
Incorrect return type
3. Given the BST below and the value 14, what is the ceil value?
10
/ \
5 15
/ \
12 20
medium
A. 12
B. 15
C. 20
D. 10
Solution
Step 1: Locate value 14 in BST context
14 is not in BST. We look for smallest value ≥ 14.
Step 2: Check nodes greater than or equal to 14
Nodes ≥14 are 15 and 20. The smallest among these is 15.
Final Answer:
15 -> Option B
Quick Check:
Ceil(14) = 15 [OK]
Hint: Ceil is smallest node ≥ target [OK]
Common Mistakes:
Choosing 12 which is less than 14
Choosing 20 which is larger but not smallest ≥14
Confusing floor and ceil
4. Consider this buggy C++ code snippet to find floor in BST:
int floorBST(Node* root, int val) {
if (!root) return -1;
if (root->data == val) return root->data;
if (root->data > val) return floorBST(root->left, val);
int floorRight = floorBST(root->right, val);
return (floorRight != -1 && floorRight <= val) ? floorRight : root->data;
}
What is the bug?
medium
A. It always returns root->data without recursion
B. It does not check for null root
C. It returns root->data when floorRight is greater than val, which is incorrect
D. It returns -1 for all values
Solution
Step 1: Understand the floor logic in code
If root->data > val, search left subtree. Else search right subtree and compare.
Step 2: Identify incorrect return condition
The code returns root->data if floorRight > val. However, since floorRight is never > val (returns -1 or ≤ val), this never triggers, causing it to return -1 (when right subtree has no floor ≤ val) instead of root->data.
Final Answer:
It returns root->data when floorRight is greater than val, which is incorrect -> Option C
Quick Check:
Floor must be ≤ val, code returns invalid value [OK]
Hint: Floor must never exceed val; check return conditions [OK]
Common Mistakes:
Returning values greater than val as floor
Missing null checks
Ignoring recursion on left subtree
5. You have a BST with nodes: 8, 4, 12, 2, 6, 10, 14. You want to find both floor and ceil for value 7. Which pair is correct?
hard
A. Floor = 6, Ceil = 8
B. Floor = 8, Ceil = 6
C. Floor = 4, Ceil = 10
D. Floor = 7, Ceil = 7
Solution
Step 1: Identify floor of 7 in BST
Floor is greatest value ≤7. Values ≤7 are 2,4,6. Greatest is 6.
Step 2: Identify ceil of 7 in BST
Ceil is smallest value ≥7. Values ≥7 are 8,10,12,14. Smallest is 8.