
Find a price in a sorted list
easyFind a price in a sorted list
Best Buy Python Interview Question
Best Buy's price-match tool checks whether a competitor's price appears in a list of past prices, which is sorted from lowest to highest and has no repeats. The list can have millions of entries, so checking every price one by one is too slow.
Write a function find_price(prices, target) that returns the position of the target price in the list, or -1 if it is not there. Positions start at 0.
Asked of
- Data Analyst
- Data Engineer
- Data Scientist
- ML Engineer
- AI Engineer
Example 1
Input
prices = [49, 99, 199, 299, 499, 999], target = 299
Output
3
Example 2
Input
prices = [49, 99, 199, 299, 499, 999], target = 250
Output
-1
Explanation
In the first example, 299 is the fourth price in the list, at position 3. In the second example, 250 falls between 199 and 299 but is not in the list, so the answer is -1.
Submit also runs 5 hidden test cases that check edge cases.
Company
Best Buy
Difficulty
easy
Topic
binary search
Language
Python
Your code
Loading Python in the background. You can start writing.