Design and Implement a Shopping Cart Price Calculator
Design and implement a price-calculator API for a cart of product identifiers, extensible discount rules, and scale-focused lookup optimizations.
Design and implement a shopping-cart price-calculator API that accepts a list of product identifiers and returns the cart's total price. Repeated identifiers represent multiple units. Product prices come from a catalog, and the design must make future discount types easy to add.
Support a configurable cart-wide percentage discount, a product-specific buy-three-get-one-free promotion, and configurable cart-total and item-count thresholds.
For this exercise, set the thresholds to an original catalog subtotal above €100 (10,000 cents) or more than 10 units; if either or both threshold conditions hold, double the configured cart-wide discount rate exactly once. Every complete group of four units of an eligible product receives one free unit.
Apply product-specific free-unit adjustments first, then the cart-wide percentage to the remaining amount. Use one currency, non-negative integer cents, basis points for percentages, arbitrary-precision intermediate arithmetic, and round half up to cents for the percentage adjustment.
An empty cart returns a zero quote. An unknown product or invalid catalog price fails the complete calculation with no partial quote. One calculation uses coherent catalog and promotion-configuration snapshots and returns an immutable quote with base lines, subtotal, labeled adjustments, and final total.
Keep the result non-negative. Promotion administration, inventory, tax, payment, checkout, persistence implementation, and distributed deployment are outside the bounded implementation.
Afterward, explain how the design would avoid scanning up to one million discount definitions per quote, how it would optimize a product that dominates traffic while configurations can change, and the resulting time and space complexity.