Per-Unit Coupon Pricing with Half-Even Cent Rounding
Arrays & HashingAuto-graded · 19 test casesLast updated
Total a shopping cart where each coupon discounts one unit of its item, with percent discounts rounded half-to-even in exact integer arithmetic.
Cart Total With One-Unit Coupons
A store is piloting a coupon program. A shopper has a cart of products and a list of scanned coupons. Each coupon discounts exactly one unit of one product. Compute the total amount owed, in integer cents.
Input
cart is a list of dictionaries, one per product line:
item_id(string): product identifier, unique within the cart.unit_price_cents(integer): price of a single unit, in cents.quantity(integer): number of units bought.
coupons is a list of dictionaries:
coupon_id(string): coupon identifier.item_id(string): the product the coupon targets.discount_type(string):"PERCENT"or"FIXED".discount_value(integer):- for
"PERCENT", a whole percentage from 1 to 100; - for
"FIXED", a number of cents taken off one unit.
- for
Rules
- One coupon, one unit. A coupon discounts a single unit of its product and cannot be reused or split. If you buy 3 units and have 2 coupons for that product, 2 units are discounted and 1 is full price.
- Ignored coupons. A coupon whose
item_idis not in the cart is ignored. If a product has more coupons than units, the extra coupons are ignored. - Order. Coupons for the same product are used in the order they appear in
coupons. The firstquantityof them are applied, and the rest are ignored. - Percent discounts. The discount in cents is the exact value
unit_price_cents * discount_value / 100. If it is not a whole number, round it to the nearest integer. A value exactly halfway between two integers goes to the even one (round-half-to-even), so22.5becomes22,37.5becomes38and2.5becomes2. - Fixed discounts. The discount is
discount_valuecents. - Floor. A discounted unit costs
max(0, unit_price_cents - discount). It is never negative. - Units without a coupon cost
unit_price_cents.
Return the sum over all units of the price actually charged, as an integer number of cents.
Constraints
0 <= len(cart) <= 10^40 <= len(coupons) <= 10^41 <= unit_price_cents <= 10^61 <= quantity <= 10^4- For
"PERCENT":1 <= discount_value <= 100 - For
"FIXED":1 <= discount_value <= 10^6 - An empty cart gives a total of
0.
Examples
Example 1
Input: compute_checkout_total([{'item_id': 'apple', 'unit_price_cents': 150, 'quantity': 2}, {'item_id': 'banana', 'unit_price_cents': 75, 'quantity': 1}], [{'coupon_id': 'c1', 'item_id': 'apple', 'discount_type': 'PERCENT', 'discount_value': 15}, {'coupon_id': 'c2', 'item_id': 'orange', 'discount_type': 'FIXED', 'discount_value': 50}])
Output: 353
Example 2
Input: compute_checkout_total([{'item_id': 'milk', 'unit_price_cents': 250, 'quantity': 1}], [{'coupon_id': 'c1', 'item_id': 'milk', 'discount_type': 'PERCENT', 'discount_value': 15}])
Output: 212
Example 3
Input: compute_checkout_total([{'item_id': 'item', 'unit_price_cents': 100, 'quantity': 3}], [{'coupon_id': 'p1', 'item_id': 'item', 'discount_type': 'FIXED', 'discount_value': 25}, {'coupon_id': 'p2', 'item_id': 'item', 'discount_type': 'PERCENT', 'discount_value': 50}, {'coupon_id': 'p3', 'item_id': 'item', 'discount_type': 'FIXED', 'discount_value': 200}, {'coupon_id': 'p4', 'item_id': 'item', 'discount_type': 'FIXED', 'discount_value': 10}])
Output: 125
Example 4
Input: compute_checkout_total([{'item_id': 'x', 'unit_price_cents': 1, 'quantity': 2}, {'item_id': 'y', 'unit_price_cents': 999, 'quantity': 1}], [])
Output: 1001
Hints
Hint 1
Group coupons by item_id, keeping their input order, or keep a per-item counter of how many coupons have been used so far. Each cart line can use at most quantity coupons.
Hint 2
Do not loop over individual units. Start from the full-price total of every line, then adjust by the difference for each coupon that is actually applied.
Hint 3
Floating point can misjudge exact halves. Use divmod(price * pct, 100) to get a quotient and a remainder in hundredths. Round up if the remainder is over 50, and on exactly 50 round up only when the quotient is odd.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Two things need care: deciding which coupons get used, and rounding percent discounts exactly.
Matching. Every unit of a cart line has the same price, so it does not matter which unit a coupon lands on. Only the choice of coupons matters. For each item, the first quantity matching coupons in list order are used. We keep a per-item counter of used coupons. When a coupon arrives:
- if its item is not in the cart, skip it;
- if the counter already equals the quantity, skip it as surplus;
- otherwise apply it and increment the counter.
Pricing without a per-unit loop. Start with the full-price total, the sum of price * quantity over all lines. Each applied coupon changes the total by max(0, price - discount) - price, which is zero or negative. This runs in constant time per coupon, even with up to 10^8 units in the cart.
Exact rounding. The percent discount is price * pct / 100. Floating point can misplace values that should land exactly on .5, so we use integers:
whole, rem = divmod(price * pct, 100)
The true discount is whole + rem/100.
- If
rem > 50, round up towhole + 1. - If
rem < 50, the result iswhole. - If
rem == 50, it is an exact tie. Pick the even neighbor:wholeif it is even, otherwisewhole + 1.
For example, 150 * 15 = 2250 gives whole=22, rem=50, a tie with 22 even, so the discount is 22 and the unit costs 128. 250 * 15 = 3750 gives whole=37, rem=50, a tie with 37 odd, so the discount is 38 and the unit costs 212.
Floor. A 100% coupon gives exactly the price, so the unit costs 0. A fixed discount larger than the price is clamped by max(0, ...).
Why it is correct
- The coupons used for each item are exactly the first
quantityin input order, as the rules require. - Each used coupon reduces one unit's charge from
priceto the discounted price. Every other unit stays at full price. - Integer arithmetic makes the tie test exact, so half-to-even rounding is never affected by representation error.
Complexity
- Time: O(N + C), where N is the number of cart lines and C is the number of coupons.
- Space: O(N) for the price, quantity and usage maps.
Reference solution
from typing import List
def compute_checkout_total(cart: List[dict], coupons: List[dict]) -> int:
prices = {}
quantities = {}
total = 0
for entry in cart:
item = entry["item_id"]
price = entry["unit_price_cents"]
qty = entry["quantity"]
prices[item] = price
quantities[item] = qty
total += price * qty
used = {}
for coupon in coupons:
item = coupon["item_id"]
if item not in quantities:
continue
count = used.get(item, 0)
if count >= quantities[item]:
continue
price = prices[item]
value = coupon["discount_value"]
if coupon["discount_type"] == "PERCENT":
whole, rem = divmod(price * value, 100)
if rem > 50 or (rem == 50 and whole % 2 == 1):
discount = whole + 1
else:
discount = whole
else:
discount = value
total += max(0, price - discount) - price
used[item] = count + 1
return total