A. It does not recurse on the right subtree when node.val > key
B. It does not handle the case when left_ceil is None correctly
C. It should check node.val > key instead of node.val < key
D. It incorrectly returns floor instead of ceil
Solution
Step 1: Recall correct ceil recursion logic
For ceil: when node.val < key recurse right subtree; when node.val > key, recurse left and return left_ceil or node.val.
Step 2: Examine code branches
Code uses else if node.val > key { ceil(&node.left, key) } (go left when > key) and else (< key) recurse right or node.val. This matches floor logic: go left when > key, right when < key updating candidate.
Step 3: Verify with trace
In Q3 BST, key=14: path 15(>14) → left 10(<14) → right 12(<14) → None → 12 → 12. Returns 12 (floor), not 15 (ceil).
Final Answer:
It incorrectly returns floor instead of ceil -> Option D
Quick Check:
Branches match floor logic [OK]
Hint: Check recursion direction matches ceil logic [OK]
Common Mistakes:
Confusing floor and ceil logic
Misreading recursion branches
Assuming missing recursion where none exists
5. You want to find both floor and ceil of a key in a BST efficiently. Which approach is best to implement in Rust?
hard
A. Traverse the BST once, updating floor and ceil candidates during the same recursion
B. Traverse the BST twice: once for floor and once for ceil separately
C. Convert BST to sorted array and use binary search twice
D. Use BFS traversal to find floor and ceil
Solution
Step 1: Consider efficiency of traversals
Traversing twice (Traverse the BST twice: once for floor and once for ceil separately) doubles time. Converting to array (Convert BST to sorted array and use binary search twice) adds extra space and time. BFS (Use BFS traversal to find floor and ceil) is not suitable for ordered search.
Step 2: Single traversal updating floor and ceil
During one recursion, compare node value with key and update floor if node.val ≤ key, update ceil if node.val ≥ key. This is efficient and uses BST properties.
Final Answer:
Traverse the BST once, updating floor and ceil candidates during the same recursion -> Option A
Quick Check:
One traversal updating floor & ceil is optimal [OK]
Hint: Update floor and ceil in one BST traversal [OK]
Common Mistakes:
Doing two separate traversals wasting time
Using BFS ignoring BST order
Converting BST to array unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.