Shopping Cart
E-Commerce Cart System
Design a shopping cart that holds items, prices them with stackable promotions and coupons, and turns them into an order without overselling stock or charging a stale price.
Overview
A shopping cart holds what a shopper intends to buy. The shopper adds and removes items, sees a price with discounts and tax, applies a coupon, and checks out. Behind that simple screen sit three hard questions: how promotions combine, how money is added up without rounding errors, and what happens when the price or the stock changes between 'add to cart' and 'pay'.
The interviewer is listening for four things. First, that the cart stores only product ids and quantities, and prices are looked up when a quote is made. Second, that each promotion is its own object behind one interface, so a new promotion is a new class and the pricing code is not edited. Third, that money is an integer number of cents. Fourth, that checkout re-prices the cart and takes stock for every line or for none.
Scoping the problem
The design starts from seven questions. Each bullet gives the question, why it matters, and the usual answer.
- Which promotion types? This decides the rule interface. Usually percent off a category, buy X get Y free, and an amount off above a minimum.
- Do promotions stack, and in what order? The order changes the total. Item rules come first, on list prices, then cart rules on what is left.
- Do coupons expire, and is each code used once? Then coupon state must be shared and guarded. Yes to both.
- Is stock held when an item is added, or only at checkout? This decides where the stock lock sits. Only at checkout.
- What if a price changes after the shopper saw the total? Charging a different amount than shown is a bug. Refuse and show the new total.
- Guest carts? They need a merge on login. Yes, and the quantities add up.
- Tax? It is one more step in the price pipeline. One rate, on the discounted amount.
Functional requirements
- add(sku, qty), set_quantity(sku, qty) where 0 removes the line, and merge(guest_cart) on login
- preview(cart, now, coupon) returns a Quote: lines, subtotal, each discount with its label, tax and total
- Promotions: percent off a category, buy X get Y free on a SKU, and an amount off when the total reaches a minimum
- Coupons have an expiry time and a use limit; an expired or used-up code is refused
- place_order(cart, now, expected_total, coupon) re-prices, refuses if the total changed, reserves stock for all lines or none, and redeems the coupon
- An out-of-stock line fails the whole order and takes nothing
Non-functional requirements
- Money is an int of cents; rounding happens only where the code divides, and the direction is stated
- A new promotion type is a new PromotionRule subclass; PricingEngine is not edited
- Every discount appears on the quote with its label, so the price is explainable
- Stock is never oversold when shoppers check out at the same time
- Time is passed in, so coupon expiry is exact in tests
Interview questions
- A marketing team wants a promotion where only the single best discount applies, not all of them. How do you support that?
- The price changed between the time the shopper saw the total and the time they clicked Pay. What do you do?
- Two shoppers try to buy the last knife at the same time. Walk me through it.
- How do you merge a guest cart into a signed-in cart?
- How would you persist carts?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio