Round Prices to Match Target
Reported by candidates from Airbnb's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Airbnb reported this one in April 2019, and the input size is the whole point: you can't try every floor-or-ceiling combination across the array, because that's 2^n choices. You get a float array and a target, and you have to round each price so the sum hits the target with the least total error. The hinted pattern is simulation, but the real move is a greedy pick on fractional parts. If the OA lands in your inbox this week, learn the trick below. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment.
The problem
Given a float array prices and an integer target, round every price to either its floor or its ceiling so that the rounded values sum to target while minimizing the total absolute rounding error. For example, prices = [1.2, 4.3, 5.8, 6.4] and target = 18 returns [1, 4, 6, 7]. Practice rule If equal fractional parts compete for the last round-up position, prefer the smaller original index. If no floor-or-ceiling assignment can sum to target, return an empty array. Function roundPricesToMatchTarget(prices: float[], target: int) → int[] Examples Example 1 prices = [1.2, 4.3, 5.8, 6.4] target = 18 return = [1, 4, 6, 7] :)
Reported by candidates. Source: FastPrep
Pattern and pitfall
Floor everything first and add up the floors. The gap between target and that sum is how many prices must round up. If the gap is negative or bigger than the count of prices with a nonzero fractional part, no assignment works, so return an empty array. Otherwise, sort indices by fractional part descending, breaking ties by smaller original index, and round up the top k. That minimizes error because rounding up a price costs (1 - frac) instead of frac, so the biggest fractions are the cheapest to bump. The pitfall is whole-number prices. Their floor and ceiling are equal, so they can't absorb a round-up and shouldn't count toward the gap. Float precision bites too, so compute fractions carefully. Sorting makes it O(n log n). During the live OA, StealthCoder is the hedge if the tie-break or the infeasible case slips your mind.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Round Prices to Match Target 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Airbnb's OA.
Airbnb reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Round Prices to Match Target FAQ
What's the trick in Round Prices to Match Target?+
Floor every price, then compute how many must round up: target minus the sum of floors. Pick the prices with the largest fractional parts to round up. That greedy choice minimizes total absolute error, since a large fraction means a small cost to go up.
How do I know when to return an empty array?+
If target minus the sum of floors is negative, you can't go lower than all floors. If it exceeds the number of prices with a nonzero fractional part, you can't go high enough. Whole-number prices can't round up, so exclude them from that count.
How are ties handled?+
If equal fractional parts compete for the last round-up slot, the smaller original index wins. Sort by fractional part descending, then by index ascending. A stable sort on index order gives you this for free.
Is brute force acceptable here?+
No. Trying every floor or ceiling combination is 2^n, which blows up fast on larger arrays. The greedy sort runs in O(n log n) and is provably optimal, so go straight to it.
How should I prepare in 48 hours for this Airbnb problem?+
Write the floor-sum, gap, and sort-by-fraction solution from memory once. Then test three cases: the sample, an infeasible target, and prices that are whole numbers. Add a tie case with equal fractions to confirm the index rule works.