Shopping cart pricing with category coupons
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Applied Intuition's technical phone screen, last asked Sep 2025. It tests object-oriented design, pricing and rules.
Build the pricing engine for a shopping cart with coupons. Each item has an id, a category and a unit_price in integer cents. A coupon targets one or more categories and has a minimum item count, a minimum price, a percentage discount (0–100) and a flat discount in cents. Return the cart's final total in cents.
Part 1: one coupon, one category
Unless you agree on a different interface with the interviewer, assume:
-
apply_coupon(items: list[dict], coupon: dict) -> int— the coupon hasapplicable_category,minimum_total_item_requirement,minimum_total_price_requirement,percentage_discount,value_discount.
Rules: collect the items in the coupon's category. The coupon applies, once, only if their count and their subtotal both meet the minimums. It discounts only that subtotal: percentage first, rounded half up to the cent, then the flat amount, and the result never goes below 0. Other items are added at full price.
items = [{id:1, A, 1000}, {id:2, A, 2000}, {id:3, B, 500}]
coupon = {A, min items 2, min price 2500, 10%, 300 off}
apply_coupon(items, coupon) -> 2900 # A: 3000 - 300 - 300 = 2400, plus B 500
items = [{id:1, A, 999}]
coupon = {A, min items 1, min price 0, 15%, 0 off}
apply_coupon(items, coupon) -> 849 # 15% of 999 = 149.85, rounds to 150
Part 2: several coupons, several categories
-
apply_coupons(items: list[dict], coupons: list[dict]) -> int— each coupon now hasapplicable_categories(duplicates count once).
Coupons are processed in order. A coupon looks at every still-unused item in its categories and checks the minimums against their combined count and subtotal. If it qualifies it applies once to that combined subtotal (same percentage, rounding and floor rules), and those items are used up for later coupons. A coupon that doesn't qualify is skipped and uses nothing.
items = [{1, A, 1000}, {2, B, 600}, {3, B, 400}, {4, C, 500}]
coupons = [{[A, B], min items 3, min price 1800, 10%, 100 off},
{[B, C], min items 1, min price 0, 20%, 0 off}]
apply_coupons(items, coupons) -> 2100 # A+B: 2000 -> 1700; then only C is left: 500 -> 400
Constraints
- Up to 2 * 10^5 items and 2 * 10^5 coupons;
0 <= unit_price <= 10^9; minimums and flat discounts up to 10^14. - All money is integer cents; half-up rounding of
S * P / 100is(S * P + 50) // 100.
Clarifying questions
- Is the percentage applied before or after the flat discount?
- How is rounding done, and are prices integers?
- Does a coupon discount the whole cart or only its eligible items?
- Can an item be discounted by two coupons?
- What happens when the flat discount exceeds the subtotal?
What a strong answer covers
- Integer-cent arithmetic with explicit half-up rounding; no floats.
- Per-category aggregates (count, subtotal) so each coupon is evaluated in time proportional to its category list, not the cart; zeroing a category once its items are used.
-
Item,Couponand an engine with clear interfaces, so new coupon rules plug in without rewriting the pricing loop. - Edge cases: no eligible items, minimums of zero, 100% off, flat discount larger than the subtotal, overlapping categories across coupons.
- Unit tests for each rule and for coupon order.
Follow-ups
- Choose the coupon order that gives the customer the lowest total.
- Add a coupon type such as "buy two, get one free" without touching existing code.
- A coupon caps its discount at a maximum amount.
Where it's asked
- Applied Intuition · Technical phone screen · Last asked Sep 202545 min · Live coding in CoderPad; the language can be fixed by the role (Python, C++, or C for embedded)
What it tests
object-oriented design · pricing · rules
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
