Find the Listed Price Closest to a Customer's Budget
Last updated: November 29, 2026
Quick Overview
Listed prices for a product family are stored in a binary search tree. Given a customer's budget as a float, return the listed price closest to it, choosing the smaller price on a tie.
Amazon
November 29, 20260
0
0 solved
Listed prices for a product family are stored in a binary search tree. Given a customer's budget as a float, return the listed price closest to it, choosing the smaller price on a tie.
An Amazon phone screen warm-up that tests whether you exploit BST ordering instead of scanning every node. It ties into price-matching features where a shopper's target price is snapped to the nearest real offer.
What the Interviewer Expects
- Walk a single root-to-leaf path using the BST property
- Track the best candidate as you descend
- Apply a clear tie-break rule
- Explain O(h) time and how it degrades to O(n) on a skewed tree
- Avoid recursion since iteration is simpler here
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
- How would you return the k closest prices instead of one?
- What if the tree is unbalanced and queries are frequent?
- How would you answer the query if prices were in 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: root of a non-empty BST of integer prices and a float target. Output: the integer price minimizing |price - target|; if two prices tie, return the smaller one....
High-Level Design
Start at the root and keep best. At each node, update best if this node is strictly closer, or equally close and smaller. Then move left if the target is smaller than the node value, otherwise rig...