Quick revision
The short answer
I would design rules as data, stored and versioned, built from a small set of rule types that implement one interface: applies(cart) and apply(cart) returning price adjustments. A deterministic, pure engine evaluates them in an explicit order with exclusive groups and caps, using BigDecimal and fixed rounding, and returns the priced cart plus an explanation of every adjustment and the rule version behind it. A strong candidate asks about scale and change rate first, avoids building a general rule language too early, and knows when a commercial or open-source rules engine would pay for itself.
What to remember
In this answer, we designed a discount engine where rules are versioned data built from a few tested rule types sharing one interface. An explicit policy of priorities, exclusive groups and caps makes evaluation deterministic, and a pure engine with BigDecimal and a single rounding rule returns both the price and a list of adjustments that explains it. Rule versions are stored with each order and new rules are dry-run against historical carts. A strong candidate asks about scale first, avoids a premature rule language, and knows when an existing rules engine is worth adopting.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would design rules as data, stored and versioned, built from a small set of rule types that implement one interface: applies(cart) and apply(cart) returning price adjustments. A deterministic, pure engine evaluates them in an explicit order with exclusive groups and caps, using BigDecimal and fixed rounding, and returns the priced cart plus an explanation of every adjustment and the rule version behind it. A strong candidate asks about scale and change rate first, avoids building a general rule language too early, and knows when a commercial or open-source rules engine would pay for itself.
Why this question is asked
Discounts are where pricing code goes to rot. Each campaign adds an if statement, two campaigns interact in a way nobody planned, and customer support cannot explain why a customer paid 47.30. Interviewers use this exercise at senior level because there is no single right answer; what matters is the boundaries we draw, how we make behaviour predictable and explainable, and whether we size the solution to the real problem. In this answer, we will clarify the requirements, design rules as data, define the stacking policy, make the engine pure and explainable, add versioning and safe activation, and finish with what a strong candidate shows.
1Questions to Ask Before Designing
A senior answer starts with questions, because they change the design. How many rules are active at once: ten or ten thousand? How often do they change: weekly, as stated, or hourly during a sale? Who writes them: engineers, or product managers in an admin screen? How many carts are priced per second at peak? Must the price shown in the cart match the price at checkout exactly, even if a rule changes in between?
Suppose the answers are: about 200 active rules, new ones weekly, written by product managers, a few hundred pricing calls per second, and the checkout price must match the price the customer saw. That points to rules as validated data in a database, evaluated in memory by a small engine, with the rule set versioned so a cart can be repriced with the same version it was shown with.
2Rules as Data, Built From a Few Rule Types
Product managers add rules weekly, so a new campaign must not need a code change and a deployment. But they also should not get a general programming language. The middle ground is a small set of rule types, written in code and tested once, whose parameters are data. Percent off a category, buy N get M free, coupon code, customer tier discount and spend threshold cover most campaigns.
Each rule type implements one interface. applies(cart) says whether the rule is relevant, and apply(cart) returns adjustments: small records saying which line or the order total changes, by how much, and why. A rule never modifies the cart itself.
Stored as JSON or table rows, a rule is then something like type PERCENT_OFF_CATEGORY, category shoes, percent 15, priority 20, group seasonal, valid from and to. The admin screen offers a form per rule type and validates the parameters, so a rule that cannot work never reaches the engine.
3Stacking, Exclusion and Order
Most surprises come from rules interacting. Does 15 percent off shoes apply before or after the 10 euro coupon? Can a tier discount stack with a seasonal sale? The engine must answer these with an explicit policy, not with whatever order the database returned.
The policy has three parts. Priority fixes the order of evaluation, because percentages applied after a fixed amount give a different result than before it. Exclusive groups say that at most one rule from a group applies; when several match, the engine picks by a stated rule, such as the best one for the customer. Caps limit the total, for example a maximum discount of 50 percent of the order, or a coupon that cannot make a line negative. With these three, the same cart and the same rule set always give the same price.
- Load the active rule-set versione.g. version 412
- Keep rules whose applies(cart) is true
- Within each exclusive group keep onebest for the customer, ties by rule id
- Apply in priority ordereach rule sees the cart after earlier adjustments
- Apply capsmax 50% of the order, no negative lines
- Round with one fixed rule
HALF_EVENto cents, per line - Return priced cart + adjustments + versionthe explanation support will read
7 steps in order. Play them, or step through them one at a time.
4A Pure Engine That Explains Every Price
The engine should be a pure function: input a cart, a customer context and a rule set, output a priced cart and a list of adjustments. It reads no clock, database or network inside; the caller passes the current time in the context. The reason is that pure code is easy to test with plain unit tests, and any historical price can be replayed exactly, which is what support and audit need.
The list of adjustments is the explanation. When a customer asks why shoes listed at 80.00 cost 58.00, support sees: line total 80.00, rule seasonal-shoes version 3 took 12.00 (15 percent off shoes), coupon WELCOME10 version 1 took 10.00, cap not reached, rounding none. Every step has a rule id, a version and a reason in plain words. We store this explanation with the order, so it is still available after the rules change.
Money uses BigDecimal with one rounding mode and one place where rounding happens, so that rounding a line and then summing gives the same answer every time. Mixing rounding per line and rounding on the total is a classic source of one-cent differences between cart and invoice.
5Versioning and Safe Activation
Every change to a rule creates a new version, and the rule set as a whole gets a version number too. The order stores the rule-set version that priced it. Then the checkout can reprice with the same version the cart was shown with, support can replay any order, and finance can see when a campaign changed.
Before a new rule goes live, we can check it against history. Taking, say, 50,000 recent carts and pricing them with the current and the candidate rule set shows how many orders change and by how much in total. If a supposedly small coupon changes 30 percent of orders, someone has made a mistake, probably a missing category filter. This dry run, plus scheduled activation with an end date, catches most expensive errors before customers do.
6What a Strong Candidate Shows
A strong candidate asks about scale and change rate before drawing boxes, because 200 rules a week and 50,000 rules a minute need different systems. They keep the rule types few and concrete and resist inventing a general rule language with its own parser, which is weeks of work and a new way to write bugs. They make order and stacking explicit and deterministic, use BigDecimal with a single rounding rule, keep the engine pure, and treat explainability and versioning as requirements rather than extras.
They also know when to buy instead of build. If the business later needs complex conditions written by non-engineers, many rule types, or a decision-table tool with its own audit trail, an established rules engine such as Drools, or a commercial promotions platform, may be worth its learning curve. A good candidate names that trade-off and the point at which they would revisit it, instead of either rejecting or adopting such a tool by reflex.
7Follow-up questions
How would you test that two rules never stack by accident?
At three levels. Unit tests per rule type check each rule alone. Policy tests build carts where two rules from the same exclusive group both match and assert that only one adjustment appears, and property-based tests generate random carts and rule combinations and assert invariants: no line below zero, total discount within the cap, at most one adjustment per exclusive group. Finally, the historical dry run before activation compares old and new prices on real carts and flags any order where the new rule stacks with an existing one.
How do you handle a rule that changes while a customer is checking out?
The cart records the rule-set version used to price it. At checkout we reprice with that version if it is still within its allowed window, so the customer pays what they saw; if the rule has expired, we reprice with the current version and show the change before payment.
8Conclusion
In this answer, we designed a discount engine where rules are versioned data built from a few tested rule types sharing one interface. An explicit policy of priorities, exclusive groups and caps makes evaluation deterministic, and a pure engine with BigDecimal and a single rounding rule returns both the price and a list of adjustments that explains it. Rule versions are stored with each order and new rules are dry-run against historical carts. A strong candidate asks about scale first, avoids a premature rule language, and knows when an existing rules engine is worth adopting.
Open with
First I would ask how many rules are active, how often they change, who writes them and the peak pricing rate, because that decides how heavy the system needs to be.
Then, if they want more
Assuming a few hundred rules written weekly by product managers, I would make rules data, not code, but built from a small set of rule types written and tested in code: percent off a category, buy N get M, coupon, customer tier, spend threshold.
Each implements an interface with applies(cart) and apply(cart), which returns adjustments: rule id, version, line, amount and a readable reason. The engine applies an explicit stacking policy: priority for order, exclusive groups where only the best rule applies, and caps such as no negative lines.
It is pure: cart, context and rule set in, priced cart and adjustments out, with BigDecimal and one rounding rule. The adjustments are the explanation support needs, and I store them and the rule-set version with every order, so any price can be replayed.
Before activating a rule, I dry-run it against recent carts and look at how many orders change. I would avoid building a general rule language early, and I would revisit a tool like Drools only if conditions become too varied for a handful of rule types.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "In a senior design exercise, design a discount rules engine for an online shop: product...": https://scrivio-interview-library.netlify.app/java/coding-problems/5ee57fbeb7/