Design a Coupon Redemption System with Total and Per-User Limits
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design an in-memory coupon redemption system that supports creating coupons and redeeming them by users.
Each coupon has a total redemption limit (the maximum number of times it can be redeemed across all users) and a per-user redemption limit (the maximum number of times a single user can redeem that coupon). The system must strictly enforce both limits.
If a coupon is added successfully, or a redemption is valid, the operation returns `true`. Otherwise it returns `false`.
Implement the following methods:
- `addCoupon(couponId: str, totalLimit: int, perUserLimit: int) -> bool`: add a new coupon with the given total redemption limit and per-user redemption limit. Return `true` if the coupon was added. Return `false` if the coupon already exists or if `perUserLimit` exceeds `totalLimit`.
- `redeem(couponId: str, userId: str) -> bool`: attempt to redeem the coupon for the given user. Return `true` if the redemption succeeds. Return `false` if the coupon does not exist, the total redemption limit has been reached, or the user has already reached their per-user limit.
Example 1:
```text
Input:
["CouponRedemptionSystem", "addCoupon", "redeem", "redeem", "redeem", "redeem", "redeem"]
[[], ["SAVE10", 3, 2], ["SAVE10", "alice"], ["SAVE10", "alice"], ["SAVE10", "alice"], ["SAVE10", "bob"], ["SAVE10", "bob"]]
Output: [null, true, true, true, false, true, false]
sys = CouponRedemptionSystem()
sys.addCoupon("SAVE10", 3, 2) # true (coupon added: total=3, perUser=2)
sys.redeem("SAVE10", "alice") # true (alice: 1/2, total remaining: 2)
sys.redeem("SAVE10", "alice") # true (alice: 2/2, total remaining: 1)
sys.redeem("SAVE10", "alice") # false (alice hit her per-user limit of 2)
sys.redeem("SAVE10", "bob") # true (bob: 1/2, total remaining: 0)
sys.redeem("SAVE10", "bob") # false (total limit reached, 0 remaining)
```
Example 2:
```text
Input:
["CouponRedemptionSystem", "redeem", "addCoupon", "addCoupon", "redeem", "redeem"]
[[], ["WELCOME", "alice"], ["WELCOME", 1, 1], ["WELCOME", 5, 2], ["WELCOME", "alice"], ["WELCOME", "bob"]]
Output: [null, false, true, false, true, false]
sys = CouponRedemptionSystem()
sys.redeem("WELCOME", "alice") # false (coupon does not exist yet)
sys.addCoupon("WELCOME", 1, 1) # true (coupon added: total=1, perUser=1)
sys.addCoupon("WELCOME", 5, 2) # false (coupon already exists)
sys.redeem("WELCOME", "alice") # true (alice: 1/1, total remaining: 0)
sys.redeem("WELCOME", "bob") # false (total limit reached, 0 remaining)
```
```hint What state to keep
Work out which counts you need per coupon and per (coupon, user) pair, and make sure a call that returns `false` changes none of them.
```
### Constraints & Assumptions
- `1 <= len(couponId), len(userId) <= 50`
- `1 <= totalLimit <= 10^6`
- `perUserLimit >= 1`. A call with `perUserLimit > totalLimit` is rejected by `addCoupon`.
- At most 10,000 calls to `addCoupon` and `redeem` combined.
### Clarifying Questions to Ask
- If `addCoupon` is rejected because `perUserLimit > totalLimit`, may the same `couponId` be added later with valid limits?
- Are coupon and user IDs case-sensitive?
- Will `redeem` be called from several threads at once, or is single-threaded access enough for now?
- Can coupons be removed, expire, or have redemptions reversed, or are they permanent once added?
### What a Strong Answer Covers
- The state kept per coupon and per (coupon, user) pair, and what it costs in memory.
- Validation in `addCoupon`, including duplicate IDs and a per-user limit above the total.
- The checks in `redeem`, and the guarantee that a rejected call leaves every counter unchanged.
- Time per operation and total space.
### Follow-up Questions
- How would you make `redeem` safe when many threads redeem the same coupon at the same moment?
- How would you enforce both limits when the counters live in a shared database and several servers handle redemptions?
- How would you support cancelling a redemption, for example when an order is refunded?
- How would you add an expiry time to coupons?
Overview: Design an in-memory coupon redemption system where each coupon has a total redemption limit and a per-user limit, with methods to add coupons and redeem them. It tests choosing the state to track, validating inputs, and enforcing both limits without changing any counter on a rejected call.