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 than scanning the whole tree.
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 current node's value, which subtree should you explore to find the floor?
You should explore the left subtree because all values in the right subtree are greater than the current node's value, which is already bigger than the target.
Click to reveal answer
What does the floor of a value in a BST represent?
AThe smallest value greater than or equal to the target
BThe exact value only if it exists
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 is greater than the current node's value, where should you look to find the ceil?
ARight subtree
BLeft subtree
CCurrent node only
DBoth subtrees
✗ Incorrect
Since the target is greater, the ceil must be in the right subtree or the current node if it matches.
When the current node's value equals the target, what is the floor and ceil?
AFloor is smaller, ceil is larger
BBoth floor and ceil are the current node's value
CFloor is null, ceil is current node's value
DFloor is current node's value, ceil is null
✗ Incorrect
Exact match means floor and ceil are the same as the current node's value.
Which property of BST helps in finding floor and ceil efficiently?
ANodes are stored in a linked list
BNodes are stored in a queue
CLeft child is always smaller, right child is always larger
DAll nodes have two children
✗ Incorrect
BST property that left child < node < right child allows efficient search.
If the target value is smaller than the current node's value, which subtree is explored to find the ceil?
ALeft subtree
BRight subtree
CCurrent node only
DNo subtree
✗ Incorrect
Since target is smaller, ceil could be the current node or in the left subtree.
Explain how to find the floor of a given value in a BST step-by-step.
Think about how BST ordering guides your search direction.
You got /6 concepts.
Describe the difference between floor and ceil in a BST with examples.
Use simple numbers to illustrate the concept.
You got /5 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 in BST is the greatest value 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, not floor. Options C and D 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 TypeScript function signatures correctly represents a function to find the ceil in a BST?
easy
A. function findCeil(key: number, root: TreeNode): number
B. function findCeil(root: TreeNode | null, key: number): number | null
C. function findCeil(root: TreeNode, key: string): number
D. function findCeil(root: TreeNode | null): number
Solution
Step 1: Check parameter order and types
Function should accept root node (possibly null) and a key number to find ceil for.
Step 2: Validate return type
Return type should be number or null if no ceil found. function findCeil(root: TreeNode | null, key: number): number | null matches this.
Final Answer:
function findCeil(root: TreeNode | null, key: number): number | null -> Option B
Quick Check:
Correct params and return type [OK]
Hint: Root first, key second, return number or null [OK]
Common Mistakes:
Swapping parameter order
Using wrong parameter types
Missing null in root type
3. Given the BST below and key = 14, what is the ceil value?
10
/ \
5 15
/ \
12 20
medium
A. 12
B. 20
C. 15
D. null
Solution
Step 1: Locate key 14 in BST or nearest nodes
14 is not in BST. Check nodes greater than or equal to 14: 15 and 20.
Step 2: Find smallest node ≥ 14
Between 15 and 20, 15 is smaller and ≥ 14, so ceil is 15.
Final Answer:
15 -> Option C
Quick Check:
Ceil = smallest ≥ 14 is 15 [OK]
Hint: Ceil is smallest value ≥ key in BST [OK]
Common Mistakes:
Choosing 12 which is less than 14
Choosing 20 which is larger but not smallest ≥ 14
Returning null when ceil exists
4. Consider this TypeScript function to find floor in BST:
function findFloor(root: TreeNode | null, key: number): number | null {
if (!root) return null;
if (root.val === key) return root.val;
const floorRight = findFloor(root.right, key);
return floorRight !== null ? floorRight : root.val;
}
What is the bug in this code?
medium
A. It returns root.val even if floorRight is null and root.val > key
B. It does not check for null root
C. It returns key instead of floor value
D. It incorrectly calls findFloor on root.left when root.val < key
Solution
Step 1: Identify missing logic for root.val > key
The code does not recurse into the left subtree when root.val > key. Instead, it always computes floorRight from the right subtree.
Step 2: Trace buggy return
When root.val > key and right subtree yields null for floorRight (common case), it returns root.val which exceeds key, violating floor definition.
Final Answer:
It returns root.val even if floorRight is null and root.val > key -> Option A
Quick Check:
Missing left recursion on root.val > key [OK]
Hint: Check return when right subtree floor is null [OK]
Common Mistakes:
Ignoring null checks
Confusing left and right subtree calls
Assuming root.val always valid floor
5. You have a BST with values [8, 4, 12, 2, 6, 10, 14]. You want to find the floor and ceil of 5. Which pair is correct?
medium
A. Floor = 2, Ceil = 8
B. Floor = 6, Ceil = 4
C. Floor = 5, Ceil = 5
D. Floor = 4, Ceil = 6
Solution
Step 1: Identify floor of 5 in BST
Floor is greatest value ≤ 5. Values ≤ 5 are 2 and 4. Greatest is 4.
Step 2: Identify ceil of 5 in BST
Ceil is smallest value ≥ 5. Values ≥ 5 are 6, 8, 10, 12, 14. Smallest is 6.