Implement an in-memory coupon service supporting: addCoupon(code, discount, expiresAt, totalLimit, perUserLimit); redeem(userId, code, now) -> success/discountApplied; and getRemaining(code, now). Choose data structures to achieve O(
-
average lookups and updates (e.g., hash maps plus a structure for expirations). Analyze time/space complexity. Handle edge cases: invalid or duplicate codes, expired coupons, zero/negative discounts, exceeding global/per-user limits, clock skew/daylight-saving near expiresAt, and idempotent retries. Follow-up: extend to support category restrictions, minimum spend thresholds, and bulk input parsing.