Find the Stored Latency Value Closest to a Target in a BST
Last updated: November 29, 2026
Quick Overview
Latency thresholds are stored as integers in a binary search tree. Given a floating-point target, return the stored value closest to it, breaking ties toward the smaller value.
0
0
0 solved
Latency thresholds are stored as integers in a binary search tree. Given a floating-point target, return the stored value closest to it, breaking ties toward the smaller value.
A Google phone screen problem that rewards using BST order to walk a single root-to-leaf path. Interviewers watch for an unnecessary full traversal and for careful tie handling.
What the Interviewer Expects
- Walk one path using BST ordering
- Track the best candidate as you go
- Define and implement the tie-breaking rule
- Achieve O(h) time and O(1) extra space
- Handle a target outside the stored range
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the k closest values instead of one.
- What changes if the BST is very unbalanced?
- How would you answer this on a sorted array instead?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: non-empty BST root and a float target. Output: the node value minimizing abs(val - target); if two values tie, return the smaller one. Values are distinct integers....
High-Level Design
The closest value must lie on the search path for target: any node off the path is separated from the target by a node on the path that is at least as close. So walk down from the root, going left w...