KatInterview

Per-Unit Coupon Pricing with Half-Even Cent Rounding

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashinghash-mapsimulationrounding

Last 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.

Rules

  1. 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.
  2. Ignored coupons. A coupon whose item_id is not in the cart is ignored. If a product has more coupons than units, the extra coupons are ignored.
  3. Order. Coupons for the same product are used in the order they appear in coupons. The first quantity of them are applied, and the rest are ignored.
  4. 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), so 22.5 becomes 22, 37.5 becomes 38 and 2.5 becomes 2.
  5. Fixed discounts. The discount is discount_value cents.
  6. Floor. A discounted unit costs max(0, unit_price_cents - discount). It is never negative.
  7. 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^4
  • 0 <= len(coupons) <= 10^4
  • 1 <= unit_price_cents <= 10^6
  • 1 <= 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.