Costliest Chocolate
Reported by candidates from Rippling's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Rippling reported this one in September 2026, and the detail that matters is in the signature: prices and weights come as jagged arrays, and the ratio has to be compared as an exact fraction. Costliest Chocolate looks like a simple filter-and-max scan, and mostly it is. The traps sit in the comparison and the tie-break. If you're taking this OA in the next day or two, you need the pattern fast: one pass, cross-multiplication, smaller productNumber on ties. StealthCoder is the safety net if you blank on the overflow detail mid-assessment.
The problem
A chocolate catalog is represented offline by aligned arrays: recordBrands[i] is the brand of chocolate i. productNumbers[i] is its unique product identifier. prices[i][j] and weights[i][j] are the price and weight in grams of variation j of that chocolate. Among chocolates whose brand exactly equals brand, evaluate the price-per-gram ratio prices[i][j] / weights[i][j] for every variation. Return the productNumber of the chocolate containing the largest ratio. Compare ratios as exact fractions. If multiple chocolates share the largest ratio, return the smaller productNumber. Function costliestChocolate(brand: String, recordBrands: String[], productNumbers: long[], prices: int[][], weights: int[][]) → long Examples Example 1 brand = "ABC" recordBrands = ["ABC","ABC","Other"] productNumbers = [20,10,5] prices = [[400,900],[500],[1000]] weights = [[100,200],[100],[10]] return = 10 For product 20, the best ratio is 900 / 200 = 4.5. Product 10 has ratio 500 / 100 = 5, which is larger. Product 5 belongs to another brand and is ignored. Example 2 brand = "Cocoa" recordBrands = ["Cocoa","Cocoa","Other"] productNumbers = [42,7,1] prices = [[6],[9],[1000]] weights = [[2],[3],[1]] return = 7 Products 42 and 7 both have price-per-gram ratio 3. The tie is resolved by the smaller product number, so the answer is 7. The higher ratio from Other is irrelevant. Constraints 1 <= recordBrands.length == productNumbers.length == prices.length == weights.length <= 10^5 1 <= sum(prices[i].length) <= 2 * 10^5 prices[i].length == weights[i].length 1 <= prices[i].length 1 <= prices[i][j], weights[i][j] <= 10^9 1 <= productNumbers[i] <= 10^18 All product numbers are unique. At least one entry has recordBrands[i] == brand. Brand comparison is case-sensitive.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is a single linear scan over an array with a custom comparator. Skip every record whose brand doesn't exactly equal the target, case-sensitive. For each variation, you hold a best fraction (bestP/bestW) and compare a new p/w by cross-multiplying: p * bestW versus bestP * w. Both products can reach 10^18, which fits in a signed 64-bit long, but it's tight, so use long and never doubles. Floating point will give wrong ties, and ties are explicitly tested in Example 2. When the ratios are equal, keep the smaller productNumber. Product numbers go up to 10^18, so store them as long too. Total work is O(total variations), around 2 * 10^5, so there's no need for sorting or a heap. The common pitfall is taking only the first variation, or averaging. If you freeze live, StealthCoder reads the problem on screen and gives you the cross-multiply solution as a hedge.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Costliest Chocolate 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 would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Rippling's OA.
Rippling reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Costliest Chocolate FAQ
How hard is Costliest Chocolate really?+
Easy to medium. The logic is one pass with a max tracker. The difficulty is in details: exact fraction comparison, 64-bit overflow limits, and the tie-break on product number. If you handle those three, it's a quick solve.
What's the trick to comparing ratios exactly?+
Cross-multiply. To check p1/w1 > p2/w2, compare p1 * w2 to p2 * w1. Each side is at most 10^9 * 10^9 = 10^18, which fits in a signed 64-bit long. Don't use doubles, because ties would break.
How do I handle ties between chocolates?+
If two ratios are exactly equal, pick the smaller productNumber. Example 2 shows this with products 42 and 7, both at ratio 3, and the answer is 7. Check equality with cross-multiplication, then compare product numbers.
Do I need to sort or use a heap?+
No. A single scan tracks the best fraction and its product number. Sorting would add log factors for nothing. Total variations are under 2 * 10^5, so linear time is plenty.
How do I prep for this in 48 hours?+
Write the scan once in your language with long types. Test Example 2 for the tie case and a case with multiple variations per chocolate. Also confirm brand matching is case-sensitive and exact. That covers the whole problem.