FastPrepRound Prices to Match Target

Round Prices to Match Target

Airbnb logoAirbnb● MediumINTERNFULLTIMEOA
Learn

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]

:)

Source note: Original source screenshot shows the Rounding in Price Breakdown statement, uniqueness guarantee, non-brute-force requirement, example, and error comparison.

More Airbnb problems

See Airbnb hiring insights
public int[] roundPricesToMatchTarget(float[] prices, int target) {
  // write your code here
}
prices[1.2, 4.3, 5.8, 6.4]
target18
expected[1, 4, 6, 7]
Checking account…