Round Prices to Match Target
Updated
Problem statement
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 = 18return = [1, 4, 6, 7]Rounding every price down gives 1 + 4 + 5 + 6 = 16, so exactly two prices must be rounded up to reach 18.
A price rounded down has error equal to its fractional part, and a price rounded up has error equal to 1 minus its fractional part. Rounding up would cost 0.8 for 1.2, 0.7 for 4.3, 0.2 for 5.8 and 0.6 for 6.4, against 0.2, 0.3, 0.8 and 0.4 when rounded down.
Rounding up 5.8 and 6.4 gives [1, 4, 6, 7] with total error 0.2 + 0.3 + 0.2 + 0.6 = 1.3. Every other choice of two prices to round up gives a larger total error, so the answer is [1, 4, 6, 7].
Constraints
1 <= prices.length <= 1000 <= prices[i] <= 1000- Each
prices[i]has at most two digits after the decimal point. 0 <= target <= 100000
Source note: Original source screenshot shows the Rounding in Price Breakdown statement, uniqueness guarantee, non-brute-force requirement, example, and error comparison.
More Airbnb interview questions
- In-Memory File SystemOA · Seen Nov 2026
- Shortest Uncommon SubstringONSITE INTERVIEW · Seen Nov 2026
- Task Management SystemOA · Seen Nov 2026
- Recipe Management SystemOA · Seen Nov 2026
- Progressive Banking SystemOA · Seen Nov 2026
- Read Across Sound TracksPHONE SCREEN · Seen Nov 2026
- Text JustificationPHONE SCREEN · Seen Nov 2026
- Credit Accounts with Scheduled TransfersOA · Seen Oct 2026
- Decode and Store StringsPHONE SCREEN · Seen Oct 2026
- Parcel Attribute Store with TTLOA · Seen Oct 2026
Similar questions at other companies
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Return Exact Cash Register ChangeBank of America · OABank of America interview questions
- Delete and Freeze GameRubrik · OARubrik interview questions
- Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions
- Max Transfer RateAmazon · OA
- Apartment Housemate PreferencesGoogle · PHONE SCREENGoogle interview questions
- Fair Electoral Vote ApportionmentBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Minimum Fuel Car AlignmentFivetran · PHONE SCREENFivetran interview questions
- Hiking Club Fastest PaceInfosys · OAInfosys interview questions
- Server Upgrade PlanningWells Fargo · OAWells Fargo interview questions