2dbi

Design a Promotion System

viaLeetCode

Problem Object-oriented design of a promotions engine: define promotion types, eligibility rules, and apply the right promotions to a cart with stacking/conflict resolution.

Requirements

  • Promotion types: percentage off, flat off, buy-X-get-Y, free shipping — extensible without touching the engine; eligibility conditions (min cart value, category, user segment, date window, coupon code); apply best/eligible promotions to an order and itemize the discounts; stacking policy (exclusive vs combinable, priority).

Core design

  • Promotion(id, condition, action, stackingPolicy, priority): CONDITION as composable specification objects (MinCartValue, InCategory, HasCoupon + And/Or/Not composites); ACTION as strategy (PercentageDiscount, FlatDiscount, BuyXGetY implementing apply(cart) → Discount).
  • PromotionEngine: filter eligible (evaluate conditions) → resolve conflicts (sort by priority; exclusive wins or best-for-customer comparison) → apply actions, accumulating an itemized DiscountBreakdown on the order.
  • PromotionRepository for definitions (promotions are data, authored by marketing); Order/Cart with line items the actions can target.

Discussion points

  • Why specification + strategy beats subclass-per-promo-with-if-chains: new promo types and rules are pure additions (OCP) — the central design point.
  • Best-combination selection when stacking is disallowed (evaluate candidates, pick max savings) vs strict priority — cost trade-off.
  • Rounding and allocation of a cart-level discount across line items (for refunds), idempotent re-evaluation, usage limits per user/promo, and audit of applied discounts.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account