Design a Coupon / Promo Code System
MediumReal World SystemsBusiness RulesStrategy PatternValidationConcurrencyUsage Limits
Problem Statement
Design a flexible coupon and promotional code system that supports multiple discount types, usage limits, user-specific coupons, and time-bound validity — used in e-commerce or food delivery platforms.
Requirements
Functional Requirements
- Create coupon codes with: type, value, min order amount, max discount cap, expiry date, usage limit
- Coupon types: PERCENTAGE_OFF (e.g., 20%), FLAT_OFF (e.g., ₹100 off), FREE_SHIPPING, BUY_X_GET_Y
- Usage limits: total uses cap (e.g., first 1000 users), per-user limit (e.g., once per user)
- Scope: applicable to all orders, specific categories, or specific products
applyCoupon(couponCode, order)→ returns discount amount and updated order- Validate: active, not expired, user eligibility, minimum order value, usage count
- Admin: create, deactivate, view usage stats per coupon
- Stacking: configurable whether multiple coupons can be applied to one order
Non-Functional Requirements
- Thread-safe usage count increment
- Singleton CouponService
Key Entities
- Coupon: code, type, value, minOrderValue, maxDiscount, expiryDate, totalUsageLimit, perUserLimit, usageCount, scope, active
- CouponUsage: couponCode, userId, orderId, usedAt, discountApplied
- DiscountStrategy (interface):
calculateDiscount(Order, Coupon)— PercentageStrategy, FlatStrategy, etc. - CouponService: Singleton; manages coupons and validations
Constraints
- Usage limit enforced atomically (no overshooting)
- Coupon application is atomic: validate → reserve → apply (or rollback)
- Expired coupons cannot be reactivated
- Percentage discount capped at
maxDiscountvalue
!Constraints
- Usage count increment is atomic — prevents exceeding totalUsageLimit
- Percentage discount capped at maxDiscount amount
- Per-user limit tracked in CouponUsage table
- Expired or inactive coupons always rejected
- Minimum order value validated before discount applied
Output
Run or submit your code to see output here.