Maximum Laptop Rating in a Price Range
Reported by candidates from Postman's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure is the whole question here. Postman reported this OA in August 2019: given laptops with prices and ratings, answer up to a million range queries for the max rating in an inclusive price window. Brute force dies instantly at 10^6 by 10^6. The move is to sort laptops by price, then answer each query with a range-maximum structure like a sparse table or segment tree plus binary search for the bounds. If you blank on the structure during the live assessment, StealthCoder runs invisibly on your desktop and can hand you the approach as a safety net.
The problem
You are given n laptops. Laptop i has price prices[i] and rating ratings[i]. For each inclusive price query [low, high], return the maximum rating among laptops whose price is between low and high. Return -1 for a query whose price range contains no laptop. Multiple laptops may have the same price; all of them are eligible for a matching query. Function maximumRatings(prices: int[], ratings: int[], queries: int[][]) → int[] Examples Example 1 prices = [1000,1100,1300,1700,2000] ratings = [300,400,200,500,600] queries = [[1000,1400],[1700,1900],[0,2000]] return = [400,500,600] The highest eligible ratings for the three inclusive ranges are 400, 500, and 600, respectively. Example 2 prices = [5,1,9] ratings = [40,10,30] queries = [[1,9],[2,8]] return = [40,40] The full range has maximum rating 40. The narrower range includes only the laptop priced at 5, which also has rating 40. Constraints 1 ≤ prices.length = ratings.length ≤ 10^6 1 ≤ queries.length ≤ 10^6 1 ≤ prices[i], ratings[i] ≤ 10^9 Each query contains exactly two values [low, high] with 0 ≤ low ≤ high ≤ 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Sort the laptops by price and carry ratings along. Now each query [low, high] maps to a contiguous slice of the sorted array. Find the left bound with lower_bound on low and the right bound with upper_bound on high, minus one. If left is past right, return -1. Otherwise you need the max over that slice. A sparse table gives O(1) per query after O(n log n) build, and a segment tree gives O(log n). Duplicate prices are fine because the binary searches take every equal price. The common pitfall is scanning the slice per query, which is O(n) each and times out. Another is mishandling the inclusive upper bound, so check Example 1 with [1700,1900]. Sparse table memory at 10^6 by 20 is heavy, so a segment tree or an offline approach may be safer. StealthCoder is your hedge if the live OA wipes your memory of the structure.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Maximum Laptop Rating in a Price Range cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Postman's OA.
Postman reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Laptop Rating in a Price Range FAQ
What's the trick in the Postman maximum laptop rating problem?+
Sort by price, then turn each price range into an index range with binary search. After that it's a range-maximum query. Use a sparse table or segment tree so each query costs O(1) or O(log n) instead of a linear scan.
How hard is this really?+
Medium. The idea is simple once you see sorted array plus range max. The difficulty is the 10^6 limits, which rule out anything quadratic, and getting the binary search bounds right on inclusive ranges.
Do duplicate prices break the approach?+
No. Sorting keeps equal prices adjacent. Use lower_bound for low and upper_bound for high so every laptop at a boundary price is included. Then the range max naturally covers all of them.
Which range-max structure should I pick?+
A sparse table is fastest per query but uses about n log n memory, which is heavy at 10^6. A segment tree uses O(n) memory and O(log n) queries, which is safer. Both pass if implemented iteratively.
How do I prepare for this in 48 hours?+
Write sorted array plus binary search plus segment tree once from scratch. Test on both examples, plus an empty range returning -1. Practice the lower_bound and upper_bound edge cases, since off-by-one errors are where this one usually fails.