As reported: PayPal Staff Engineer Full Stack, specialization round (2025): shopping cart design — add, view and delete items; handle edge cases such as out-of-stock items, cart overflow and checking out an empty cart; object-oriented design with basic validation.
Design the classes for a shopping cart:
addItem(productId, quantity),removeItem(productId)(and changing a quantity),viewCart()with line totals and the cart total, andcheckout().- Validate every operation and fail with a clear error for: a product that is out of stock (or has less stock than requested), a cart that would exceed its limit, a quantity that is not positive, and checkout of an empty cart.
Show the classes, their responsibilities and the main methods; write the core methods in a language of your choice.
Clarifications
- The cart has a maximum number of items (sum of quantities), e.g. 50.
- Stock is checked when adding and again at checkout (it may have changed in between).