Promotion Validation: Validate and Apply Discount Promotions With Stacking and Exclusion Rules
Interview Experience
Problem
An e-commerce checkout applies promotional codes. Each promo has: a discount type (percent or flat), a value, minimum cart total to qualify, and an exclusive flag (exclusive promos cannot be stacked with others). Given a cart total and a list of applied promo codes, validate and compute the final discounted price.
python
def apply_promotions(
cart_total: float,
promo_codes: list[str],
promo_db: dict # {code: {type, value, min_cart, exclusive}}
) -> dict:
"""
**Returns** {
"final_price": float,
"applied": [codes],
"rejected": {code: reason}
}
"""
Example:
promo_db = {
"SAVE10": {"type":"percent","value":10,"min_cart":50,"exclusive":False},
"FLAT20": {"type":"flat", "value":20,"min_cart":100,"exclusive":True}
}
apply_promotions(120.0, ["SAVE10","FLAT20"], promo_db)
-> {"final_price": 88.0, "applied":["FLAT20"], "rejected":{"SAVE10":"exclusive conflict"}}
Follow-ups
- If multiple non-exclusive promos are applied, what order should percent vs. flat discounts be applied? Does order matter?
- How do you handle a promo that would make the final price negative?
- How would you design the promo schema to support "buy X get Y free" style promotions?
- If promo validation logic is complex, how do you test it without integration testing the whole checkout?
Full Details
Problem
An e-commerce checkout applies promotional codes. Each promo has: a discount type (percent or flat), a value, minimum cart total to qualify, and an exclusive flag (exclusive promos cannot be stacked with others). Given a cart total and a list of applied promo codes, validate and compute the final discounted price.
python
def apply_promotions(
cart_total: float,
promo_codes: list[str],
promo_db: dict # {code: {type, value, min_cart, exclusive}}
) -> dict:
"""
**Returns** {
"final_price": float,
"applied": [codes],
"rejected": {code: reason}
}
"""
Example:
promo_db = {
"SAVE10": {"type":"percent","value":10,"min_cart":50,"exclusive":False},
"FLAT20": {"type":"flat", "value":20,"min_cart":100,"exclusive":True}
}
apply_promotions(120.0, ["SAVE10","FLAT20"], promo_db)
-> {"final_price": 88.0, "applied":["FLAT20"], "rejected":{"SAVE10":"exclusive conflict"}}
Follow-ups
- If multiple non-exclusive promos are applied, what order should percent vs. flat discounts be applied? Does order matter?
- How do you handle a promo that would make the final price negative?
- How would you design the promo schema to support "buy X get Y free" style promotions?
- If promo validation logic is complex, how do you test it without integration testing the whole checkout?
About This Question
This is a candidate experience report from a draftkings interview during the phone round.