InterviewDB Question

Coupon System - Apply and Stack Discount Coupons on an Order

Question Details

Problem

Design a coupon application system for an e-commerce checkout. Coupons have types: percent_off, fixed_off, buy_x_get_y_free, and category_discount. Rules:

  • At most 2 coupons can be applied per order.
  • Coupons cannot reduce the price below $0.
  • Some coupons are exclusive (cannot stack with others).

Implement:
- apply_coupons(order, coupon_codes) -> CheckoutResult

python
class CouponSystem:
    def apply_coupons(
        self,
        order: dict,          # {"items": [{"id", "category", "price", "qty"}]}
        coupon_codes: List[str]
    ) -> dict:                # {"original": float, "discount": float, "final": float, "applied": List[str]}
        ...

Example:

order = {"items": [{"id":"i1","category":"electronics","price":200,"qty":1}]}
coupons = ["SAVE10PCT",  "TECH20"]
# SAVE10PCT: 10% off entire order
# TECH20: $20 off electronics category
apply_coupons(order, coupons) ->
{"original": 200, "discount": 40.0, "final": 160.0, "applied": ["SAVE10PCT","TECH20"]}

Follow-ups

  1. How do you determine the optimal order to apply coupons to maximize the discount?
  2. How do you enforce the exclusivity constraint when 3+ coupons are submitted?
  3. How would you validate coupon expiry and single-use restrictions?
  4. How do you handle a buy_x_get_y_free coupon across multiple cart items?

Full Details

Problem

Design a coupon application system for an e-commerce checkout. Coupons have types: percent_off, fixed_off, buy_x_get_y_free, and category_discount. Rules:

  • At most 2 coupons can be applied per order.
  • Coupons cannot reduce the price below $0.
  • Some coupons are exclusive (cannot stack with others).

Implement:
- apply_coupons(order, coupon_codes) -> CheckoutResult

python
class CouponSystem:
    def apply_coupons(
        self,
        order: dict,          # {"items": [{"id", "category", "price", "qty"}]}
        coupon_codes: List[str]
    ) -> dict:                # {"original": float, "discount": float, "final": float, "applied": List[str]}
        ...

Example:

order = {"items": [{"id":"i1","category":"electronics","price":200,"qty":1}]}
coupons = ["SAVE10PCT",  "TECH20"]
# SAVE10PCT: 10% off entire order
# TECH20: $20 off electronics category
apply_coupons(order, coupons) ->
{"original": 200, "discount": 40.0, "final": 160.0, "applied": ["SAVE10PCT","TECH20"]}

Follow-ups

  1. How do you determine the optimal order to apply coupons to maximize the discount?
  2. How do you enforce the exclusivity constraint when 3+ coupons are submitted?
  3. How would you validate coupon expiry and single-use restrictions?
  4. How do you handle a buy_x_get_y_free coupon across multiple cart items?

About This Question

This is a reported interview question from a plaid interview during the phone round.

It covers the following topics: Coding, Phone, Onsite, Stack .