Design a Shopping Cart with Coupons
🛒 What is a shopping cart with coupons? It is the part of an online store between "add to cart" and "pay". It remembers what each user wants to buy, checks that the store has enough stock, applies the store's automatic offers and the user's coupon code, and at checkout takes the stock and charges the right amount.
This prompt comes in several shapes: "design the checkout of an online store", "design offers and discounts for a cart", or "design coupon management where a coupon can apply to one product or one seller and has an expiry date and a usage limit". They are one design. The cart and checkout are the frame, offers and coupons are the pricing rules inside it, and scope and validity are the parts of a coupon that need real modelling. We build all of it as one system.
Requirements
"Design a shopping cart for a marketplace. Users add products, the store runs offers, and users can enter coupon codes before they check out."
The prompt says nothing about how discounts combine or when a coupon stops working, and those are exactly the decisions that shape the code.
Clarifying Questions
You: Does adding an item to the cart reserve the stock?
Interviewer: No. We check that enough stock exists when you add it, but stock is only taken at checkout. If two people want the last unit, whoever checks out first gets it.
You: What is the difference between an offer and a coupon?
Interviewer: An offer belongs to a product and applies automatically, like "buy 2, get 1 free". A coupon is a code the user types in. It takes a percentage or a fixed amount off.
You: What can a coupon cover?
Interviewer: The whole cart, one product, or every product from one seller. Sellers run their own coupons.
You: How many coupons can one cart have?
Interviewer: One. Applying a new code replaces the old one.
You: When is a coupon valid?
Interviewer: It has a start and an end time, a total number of uses, and each user can use it only once. It also needs at least one item in the cart that it covers.
You: If a product has an offer and a coupon covers it, which comes first?
Interviewer: Offers first. The coupon works on what is left. A flat coupon can never take off more than the items it covers are worth.
You: What if a coupon stops being valid after the user applied it? It expires, or they remove the item it was for.
Interviewer: Then it just doesn't count. Prices are always computed fresh. Pass the time into every call so we can test it.
The last answer is the one that changes the design most. "Validate when applied" and "validate every time we price" are different systems, and the second one is what real stores do: a cart can sit for days.
💡 Interview tip: In any pricing problem, ask two questions early: in what order do discounts apply, and when are they checked? The order decides the numbers, and the timing decides where the validation code lives.
Final Requirements
- Sellers list products with a unit price (in cents) and a stock count.
- Users add and remove units in their own cart. A cart cannot hold more units of a product than are currently in stock.
- A product can have one automatic offer: buy X, get Y free.
- Coupons take a percentage or a flat amount off. A coupon covers the whole cart, one product, or one seller's products.
- A coupon is valid within a time window, up to a total number of uses, once per user, and only if the cart has an item it covers.
- A cart has at most one coupon. A new one replaces the old one; a coupon that fails to apply leaves the old one in place.
- Pricing applies offers line by line first, then the coupon to the covered lines. A flat coupon is capped at the covered amount. Everything is re-checked every time the cart is priced.
- Checkout is all or nothing: if any line is out of stock, nothing changes. Otherwise stock is taken, the coupon is redeemed, and the cart is emptied.
- New kinds of offers, scopes and discounts will come, so adding one must not touch the cart or checkout code.
Out of scope
- Payment, tax, shipping and order tracking (see /low-level-design/order-management).
- Stock reservations with timeouts (we discuss them in Extensibility).
- Several coupons on one cart (also in Extensibility).
- Persistence and user accounts: a user is just an id.