Design and Implement a Shopping Cart Object Model for an Online Store
Company: Amazon
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Onsite
Design and implement a shopping cart for an online store, using object-oriented code in a language of your choice. This is a phone-screen object-oriented design exercise, so the goal is a small, runnable model rather than a distributed system.
The cart must let a shopper:
- add a product to the cart with a quantity;
- change the quantity of a product that is already in the cart;
- remove a product from the cart;
- list what is in the cart; and
- get the cart's total price.
Products come from a catalog that the cart can look up by product ID. Each product has at least an ID, a display name and a unit price. Write the classes, implement the operations, and show a short usage example or tests that exercise them.
```hint Separate the product from the line
A catalog product and a line in the cart change for different reasons. Decide which object owns the price and which owns the quantity before you write any methods.
```
### Constraints and Clarifications
- Assume one shopper using one cart in one process. Persistence, checkout and payment are out of scope unless the interviewer extends the problem.
- Money must be computed exactly; state how you represent it.
### Clarifying Questions
- If the shopper adds a product that is already in the cart, should the quantities be merged, or should the call replace or reject the existing line?
- Should setting a quantity to zero remove the line, or is zero an error?
- Should an unknown product ID, a negative quantity or a non-integer quantity raise an error, be ignored, or be clamped?
- Is a product's price fixed when it is added to the cart, or read from the catalog when the total is computed?
- Does the cart need to check inventory when items are added, or only at checkout?
- Are discounts, coupons or taxes part of the total in this version?
### What a Strong Answer Covers
- Clear responsibilities for catalog products, cart lines and the cart itself
- An explicit, consistent policy for duplicate adds, zero and invalid quantities, and unknown products
- Exact money arithmetic with no floating-point rounding errors
- Data structures that give constant-time updates and a predictable listing order
- A seam for pricing rules such as discounts or tax that does not require rewriting the cart
- A runnable demonstration or tests that reach the edge cases, not only the happy path
### Follow-up Questions
- Add promotions such as "buy one, get one free" on a product and a percentage-off coupon on the whole cart. Where does that logic live, and how do you decide the order in which they apply?
- A product's catalog price changes while it sits in someone's cart. What does the shopper pay at checkout, and how does your model make that explicit?
- The same cart is updated from two browser tabs at once. How do you prevent a lost update?
- How would you persist the cart so that it survives the shopper signing out and back in on another device?
Overview: An object-oriented design exercise that asks you to model and implement a shopping cart for an online store, covering adding, updating and removing products and computing the total. It tests class boundaries, explicit edge-case policies, exact money handling and how cleanly the design extends to promotions.