Design Shopping Cart (Low-Level Design)
Already solved, with comments: add items, apply tier discounts, then coupons. Run it and submit it to see how grading works. Focus: A solved example: see the whole loop before you try one.
| Difficulty | EASY |
|---|---|
| Design focus | A solved example: see the whole loop before you try one |
| Automated grading | 3 public + 10 hidden tests (13 total) |
| Phase 2 follow-up | Coupons |
| Language | TypeScript (OOP / Machine Coding) |
Problem statement
Build a shopping cart. Prices are in cents (whole numbers). Customers have a tier: BASIC pays full price, GOLD gets 10% off, PLATINUM gets 20% off.
The discount is rounded down to a whole cent. A sku can be in the cart at one price only.
Functional requirements
- add(sku, price, qty = 1) adds items, or more of an item already in the cart; it throws for a negative or fractional price, a quantity below 1, or a different price for the same sku.
- remove(sku, qty?) removes some, or with no quantity all, of an item; it throws for an unknown sku or more than is in the cart.
- count() is the number of items, counting quantities.
- total(tier) is the price after the tier discount.
Required API & starter interface
Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:
type Tier = "BASIC" | "GOLD" | "PLATINUM";
class Cart {
add(sku: string, price: number, qty: number = 1): void {
throw new Error("Not implemented");
}
remove(sku: string, qty?: number): void {
throw new Error("Not implemented");
}
count(): number {
throw new Error("Not implemented");
}
total(tier: Tier): number {
throw new Error("Not implemented");
}
}
Public test cases
Your code runs against 3 public tests plus 10 hidden edge-case tests in a sandbox:
Items are counted with their quantities
var cart = new Cart();
cart.add("APPLE", 100, 3);
cart.add("PEAR", 250);
assertEqual(cart.count(), 4);
assertEqual(cart.total("BASIC"), 550);
GOLD customers get 10% off
var cart = new Cart();
cart.add("TV", 10000);
assertEqual(cart.total("GOLD"), 9000);
Bad input is refused
var cart = new Cart();
assertThrows(function () { cart.add("X", -1); });
assertThrows(function () { cart.add("X", 100, 0); });
Design & structure checks
Beyond passing tests, the grader inspects your class structure for clean object-oriented design:
- Discounts live behind their own interface or class, not inside the cart: Discount rules change more often than the cart does. Behind an interface, a new kind of discount is a new class, and the cart stays untouched.
- Customer tiers are a named type, not loose strings: A named type lets the compiler catch a typo like "GLOD" and shows every place a new tier must be handled.
- Each line in the cart is its own small class: A line knows its product, price and quantity. Keeping that out of the cart keeps both classes small and easy to test.
Phase 2: Change request
In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 13 tests, you unlock the change request: Coupons.
Worked walkthrough
This problem is open as a solved example so you can see how clean separation of responsibilities handles both the initial spec and the change request:
Read the problem, list nouns and verbs
Nouns: cart, item, discount, tier. Verbs: add, remove, count, total. The verbs are the required methods; the nouns are candidate classes.
Spot what will change
Discount rules change often (sales, tiers, coupons). So they sit behind a Discount interface, and Cart never knows how a discount is worked out.
Draw it
Diagram mode shows this design drawn: Cart owns LineItems and uses Discounts; TierDiscount implements Discount. Press Check design to see it scored.
Write it, run it
Code mode holds the solved code, with a comment on every choice. Press Run tests: all visible tests pass. Change a number and run again to see what a failure looks like.
Submit
Submit for grade runs the hidden tests too, then the design checks. You get a score out of 100 and a note on each part.
The change request
Open 2. Change request: coupons. They were added as two new Discount classes and a table of codes. Cart gained one method, which is why this design scores well on change.
Relevant low-level design lessons
Strategy: swap behaviour, not code
Replace an if/else on a type with interchangeable strategy classes.
From a problem to classes
Turn a problem statement into a first list of classes and methods.
Hide state behind methods
Protect an object's rules by keeping its data private.
More low-level design problems
Design Parking Lot
Park vehicles in the right size of spot, issue tickets and charge on exit.
Design Tic-Tac-Toe (N × N)
Any board size, with a win check that only looks around the last move.
Design Vending Machine
Take coins, sell products, give change, and refuse what a state does not allow.
Design Snake and Ladder
A board game where the board, the players and the rules for a turn are separate pieces.