Designing a Discount and Coupon Engine
Difficulty: Intermediate Patterns: Chain of Responsibility, Specification, Strategy Asked at: Amazon, Flipkart, Myntra, Shopify
A cart, a pile of live promotions, a box where the shopper types a code, and one number at the bottom of the page. Interviewers frame it as “apply the discounts and show the total”, which sounds like a loop over a list of rules, and the first version candidates write is exactly that: a for loop subtracting each rule’s amount from a running total. It produces a number. The number is wrong, and nothing in the code can tell you so.
It is wrong because the sequence the rules ran in is part of the answer and nobody wrote it down. Take a ₹1000 cart with two promotions live: 20% off, and ₹100 off. Run the percentage first and the cart goes 1000 to 800 to 700. Run the flat amount first and it goes 1000 to 900 to 720. Same cart, same two rules, ₹20 apart, and which one the shopper gets depends on the order rules came out of a config file or a database query. That is a pricing bug whose only symptom is a revenue report.
The second hard part is that the money has to reconcile to the cent, and percentages are where that quietly fails. A ₹100-off coupon applies to the cart, but tax is per line and so is a return, so that ₹100 has to be split across the lines in a way that sums back to exactly ₹100 with no paisa invented and none lost. Do it in double and you get 99.99999999999999; take the percentage per unit and multiply by quantity and you are off by a paisa against the same percentage taken on the line total. One paisa an order across a million orders is a reconciliation ticket somebody has to close.
The third is the one candidates never volunteer and interviewers always ask: “why didn’t my coupon work?” A rule that silently does not fire is indistinguishable from a bug, so every evaluation has to come back with a verdict per rule and a reason – not eligible and which condition failed, suppressed by a higher-priority exclusive offer, capped at its maximum, expired, out of redemptions. The engine’s output is not a number. It is a number plus an explanation for every rule that did not contribute to it.
💡 Stacking = applying more than one discount to the same cart. The three policies that matter are exclusive, where one rule wins alone, stackable, where rules compose, and best-of, where several candidates are priced and only the largest saving is applied.
💡 Minor units = whole-number paise or cents, so ₹12.34 is the integer 1234 and never 12.34. Every amount here is a long of minor units because binary floating point cannot represent a tenth. Learn more →
💡 Specification = one business condition packaged as an object with an isSatisfiedBy method, so “cart total at least ₹1500” is a named, testable thing that composes with and, or and not instead of being an if. Learn more →
Functional Requirements
- Rule catalogue – percent off, flat amount off, buy X get Y free and free shipping, each a rule object with its own parameters rather than a branch in a pricing method.
- Ordered evaluation – every rule carries an explicit priority, the engine evaluates in that order, and the order is printable so a merchandiser and a test can both read it.
- Stacking policy – each rule declares itself exclusive, stackable or best-of, and the engine honours all three in one pass over the same cart.
- Eligibility conditions – composable specs such as minimum cart total, contains a category, first order and member tier, combined with and, or and not, and nameable as one composite.
- Coupon codes with usage limits – a typed code resolves to a rule, and a per-coupon and per-user redemption limit is checked and consumed atomically.
- Caps – a maximum discount per rule and a maximum total discount per cart, neither of which may take a line below zero.
- A verdict per rule and a breakdown per line – APPLIED, CAPPED, NOT_ELIGIBLE with the failing condition named, SUPPRESSED with the suppressing rule named, NOT_BEST, EXPIRED or LIMIT_REACHED, plus gross, discount, taxable base, tax and net for every line.
Non-Functional Requirements
- Evaluation commits nothing –
plan(cart)is a pure function of the cart, the rule set and the clock. It returns a plan; applying it is a separate step. That is what makes best-of possible at all, because comparing two rules means pricing both without either one taking effect. - Money reconciles exactly – all arithmetic is on
longminor units, and two invariants are asserted on every plan: the per-line discounts sum to the cart discount to the paisa, and no line is discounted below zero.doubleis disqualified, not discouraged. - Deterministic, and linear in the obvious thing – the same cart, rule set and clock give the same total, allocation and verdict order, because priority ties break on rule id and the allocation leftover goes to a defined line rather than wherever a hash iteration landed. O(R log R) to sort R rules once, then O(R x L) per cart of L lines plus O(L log L) per allocation.
- Redemption is atomic – consuming a usage slot is one compare-and-set, so a coupon capped at 100 redemptions stops at 100 under any number of concurrent checkouts, and a retried checkout carrying the same order id consumes nothing extra.
Discussion
Newest first