Round prices in e-commerce system
Anonymous User
437

In an e-commerce system, when someone buys multiple items, you show the cost of each item and the total cost of all items right before they check out.
To make you interface friendly, you round all the price to dollar.

This can sometimes create confusion, when we round up. Example when some one buys 2 items that both cost 10.410
Item 2: 21

Because we rounded each item down to 21

Write a function that take a list of items price, prices and find the floor or ceiling of each price[i],
so that the adjusted subtotals sum to rounded total price and the adjusted subtotals are also as close to the original subtotals as possible.
Phrased mathematically, you should minimize the sum of the absolute differences of the adjusted subtotals from original subtotals.
Return the list with the adjusted subtotals.

Example:
Input: prices[] = {5.40, 3.30, 5.00}
Output: {6, 3, 5}
Explanation:
5.40 + 3.30 + 5.00 = 13.70 is rounded to 14.00
=> Adjusted prices: 6 + 3 + 5 = 14

My solution is:

  • Calculate rounded total prices of bill
  • Sort prices by floating number. I mean: 0.40 -> 0.30 -> 0.00
  • Round up for each item in sorted prices until total bill equal expected rounded total price

Do you have any other ideas or solutions?
(In my coding challenge, I don't have enough time to implement my solution. So, I failed.)

Comments (0)