Senior Backend Engineering Take-Home (2-Hour Timebox)
You are given a working Spring Boot application with a stubbed pricing engine. Implement the stateless promotion engine behind POST /api/v1/cart/evaluate so that it satisfies this specification and passes the test suite.
- Timebox: 2 hours.
- AI assistants: Allowed. You own every line you submit—keep the design clean enough to walk through and extend live in a 30-minute follow-up (where you may be asked to add a new rule type without AI).
- How we evaluate: Passing the fixtures is the baseline. We look closely at:
- Extensibility & design: Clean separation between rule handlers, cart/unit state, and money allocation—not one giant
switchblock. - Correctness under rule interactions: Bundle leftovers, mix-and-match unit splits, spend-tier downgrades on a running subtotal, non-stackable cart discounts, per-line rounding, and one-cent remainders.
- Your own tests: Targeted tests covering edge cases that the 6 public fixtures do not exercise.
- Extensibility & design: Clean separation between rule handlers, cart/unit state, and money allocation—not one giant
- No external dependencies: No database, network calls, or Docker. Java 21 and the included Maven Wrapper are all you need.
./mvnw test # fast feedback loop
./mvnw spring-boot:run # starts on http://localhost:8080When calling POST /api/v1/cart/evaluate manually, send the fixture's request object ({ "cart": ..., "promotions": ... }), not the outer fixture envelope.
- API & validation: Request/response DTOs, input normalization (
EvaluationMapper), and RFC 7807ProblemDetailHTTP 400 responses for malformed JSON and bean-validation failures. - Engine seam:
PromotionEngineand a placeholderDefaultPromotionEnginethat prices each line atMoney.extended(unitPrice, quantity)and emits a placeholderSKIPPEDentry per rule. - Tests:
CartEvaluationWebTest(undiscounted cart, empty cart, HTTP 400s—keep enabled) andPromoEngineFixtureTest(6 public fixtures insrc/test/resources/fixtures/public, currently@Disabled).
- Replace the stub in
DefaultPromotionEngine(using package-private types undercom.company.promoengine.engine). Keep a singlePromotionEngineSpring bean. - Remove
@DisabledfromPromoEngineFixtureTestand make./mvnw testpass. Do not edit the public fixture files. - Add your own tests for edge cases beyond the 6 public fixtures.
- Fill in
DECISIONS.md(a few sentences per heading).
com.company.promoengine
api/ CartEvaluationController, EvaluationMapper, dto/
domain/ Cart, CartItem, PromotionRule, RuleType, EvaluationResult, AuditEntry, Money
engine/ PromotionEngine, DefaultPromotionEngine <-- implement here
exception/ GlobalExceptionHandler (RFC 7807 ProblemDetail)
Stack: Java 21, Spring Boot 3.5.16, Maven. Money uses BigDecimal (scale 2, HALF_UP). Timestamps use Instant.
The engine runs a single deterministic sequential pass—do not search for an optimal or cheaper subset of rules.
- Initialize unit state: Each cart line represents
quantityunits starting atunitPrice, withoriginalExtendedPrice = Money.extended(unitPrice, quantity). A line's current price is the sum of its units' current prices. - Sort promotions once:
priorityascending (1runs before10)idascending lexicographically (String.compareTo, soPROMO_Aruns beforePROMO_Z)- original request index (stable tie-breaker)
- Evaluate each rule once: Run the gatekeeper checks in order, apply any discount immediately to the running cart state, and record exactly one
AuditEntry(APPLIEDorSKIPPED) in evaluation order. - Build response: Keep
itemizedBreakdownin original request order and satisfy all scale-2 accounting invariants.
-
Unit-level tracking (bundles & mix-and-match)
- A
BUNDLE_FIXED_PRICErule consumes complete component sets and marks those units as bundled. Later item-level rules skip bundled units, while leftover units on the same line stay eligible at their current price. - Example: 3 laptops, 2 mice, and 2 bags form 2 bundles. The 2 bundled laptops are excluded from later item rules, while the 1 leftover laptop still qualifies for a later 10%
LAPTOPcategory rule. BUY_X_GET_CHEAPEST_FREEdiscounts individual free units on a multi-quantity line rather than discounting the entire line.
- A
-
Running subtotal &
stackableWithItemPromos- Applied rules reduce affected prices immediately, so later rules always evaluate against the running subtotal rather than the original subtotal (e.g., an item discount can drop a cart below a higher spend tier, and two 10% category rules on
100.00grant10.00then9.00). - Once an item-level rule lowers a line's current price, that line is considered item-discounted.
- Cart-level rules with
stackableWithItemPromos: falseapply only to lines that are not item-discounted. WhenstackableWithItemPromosistrueornull, the rule applies to all lines.
- Applied rules reduce affected prices immediately, so later rules always evaluate against the running subtotal rather than the original subtotal (e.g., an item discount can drop a cart below a higher spend tier, and two 10% category rules on
-
Exclusive groups
- A non-blank
exclusiveGroupis claimed only when a rule isAPPLIED. ASKIPPEDrule (out of window, invalid config, failed predicate, or0.00discount) never blocks later rules in the same group.
- A non-blank
-
Rounding & penny allocation
- Percentages: Round once to scale 2 (
HALF_UP) at the end of each line calculation (for category rules) or cart calculation (for cart rules). Do not compute a category percentage on the combined sum of multiple lines (e.g., two1.25lines at 15% round independently to0.19and0.19). - Pro-rata penny allocation: Used whenever a single total discount must be split across multiple lines (cart-level grants, bundle savings, and capped category discounts). Allocate the discount in integer cents proportionally to each eligible line's weight using the largest-remainder (Hamilton) method: assign each line its integer-cent floor share, then distribute leftover cents one at a time to the eligible lines with the largest fractional remainders whose share is still below their weight, breaking ties in favor of the earlier line in request order.
- Example: Splitting
10.00across three equal lines produces3.34,3.33,3.33. - Zero floor: A grant never exceeds its eligible base, so neither a line nor the cart total can drop below
0.00.
- Percentages: Round once to scale 2 (
For each rule in sorted order, stop at the first matching outcome and emit one AuditEntry:
- Inverted window:
validFromis aftervalidTo→SKIPPED - Validity window:
evaluationTimestampis outside inclusive[validFrom, validTo]→SKIPPED - Exclusive group: non-blank
exclusiveGroupis already claimed →SKIPPED - Configuration: missing or illegal fields for this rule type →
SKIPPED(not an HTTP 400) - Predicate: rule condition is not satisfied (no matching items/units, incomplete bundle, coupon/payment mismatch, no qualifying tier, empty eligible base) →
SKIPPED - Zero discount: computed discount is
0.00→SKIPPED - Apply: reduce affected prices immediately, claim non-blank
exclusiveGroup, emitAPPLIED
Every AuditEntry must include a clear, non-blank human-readable reason explaining why the rule was applied or skipped (and never the placeholder stub string). The fixture tests validate exact prices, line splits, rule order, status, and discountAmount, and assert that reason is non-blank rather than matching exact wording.
percentage:> 0and<= 100buyQuantity,freeQuantity: integers>= 1bundlePrice, tierminSubtotal:>= 0amount,maxDiscountAmount:> 0tiers: non-empty; every tier hasminSubtotal >= 0and exactly one of validpercentageor validamountcomponents: non-empty; every component has a non-blankskuandquantity >= 1, and eachskuappears at most once- Required fields per rule type (
category,skus,couponCode) must be non-blank / non-empty (with no blank entries inskus)
- Inputs:
conditions.category,discountConfig.percentage, optionaldiscountConfig.maxDiscountAmount. - Behavior:
- Match lines in
conditions.category(case-sensitive) that have non-bundled units. - For each matching line independently, compute
percentage%of that line's non-bundled current price (rounded once, scale 2HALF_UP, capped at the line's eligible base). - If
maxDiscountAmountis set and the sum of line discounts exceeds it, cap the rule total atmaxDiscountAmountand allocate that cap across the matching lines via penny allocation weighted by each line's uncapped discount.
- Match lines in
- Inputs:
conditions.skus,discountConfig.buyQuantity,discountConfig.freeQuantity. - Behavior:
- Pool all non-bundled units whose
skuis inconditions.skus(case-sensitive). Works for both single-SKU and multi-SKU pools. - Every complete group of
buyQuantity + freeQuantityunits earnsfreeQuantityfree units (e.g., buy 2 get 1: 1–2 units → 0 free, 3–5 → 1 free, 6–8 → 2 free). - Sort pooled units by current unit price ascending, then line order ascending, then unit index within the line ascending.
- Free the first
freeUnitsunits at their full current unit price. Each affected line's discount is the sum of its freed units' prices.
- Pool all non-bundled units whose
- Inputs:
conditions.components({ sku, quantity }),discountConfig.bundlePrice. - Behavior:
- Form as many complete sets as the cart supports (
min(floor(lineQuantity / requiredQuantity))across components; a missing SKU forms 0 sets). - Per-bundle savings equal the sum of component
unitPrice * requiredQuantityminusbundlePrice. - Allocate total savings (
saveEach * bundleCount) across the component lines via penny allocation weighted by each component's consumed value (unitPrice * requiredQuantity * bundleCount). - Mark the
requiredQuantity * bundleCountconsumed units on each component line as bundled; leftover units stay unbundled and eligible for later item-level rules.
- Form as many complete sets as the cart supports (
All three cart-level rules follow the same flow:
- Select eligible lines: all lines if
stackableWithItemPromosistrueornull, or only non-item-discounted lines iffalse. - Compute
base= sum of current prices of those eligible lines. - Compute the cart grant (always capped at
base). - Allocate the grant across eligible lines via penny allocation weighted by each line's current price, and reduce their current prices immediately.
- Inputs:
discountConfig.tiers(each withminSubtotaland eitherpercentageoramount). - Behavior: Select the single tier with the greatest
minSubtotal <= baseregardless of list order (if two tiers share the sameminSubtotal, pick the earlier one in the list). A percentage tier grantspercentage%ofbase(rounded to scale 2, capped atbase); a fixed tier grantsmin(amount, base).
- Inputs:
discountConfig.percentage. - Behavior: Grants
percentage%ofbase(rounded to scale 2, capped atbase). Skipped when no eligible subtotal remains (base == 0.00).
- Inputs:
conditions.couponCode, optionalconditions.paymentMethod, anddiscountConfig.percentageordiscountConfig.amount. - Behavior: Match
cart.couponCodecase-insensitively. If the rule setsconditions.paymentMethod, also matchcart.paymentMethodcase-insensitively; otherwise ignore the cart's payment method. Applies the percentage or fixed amount againstbase, capped atbase.
currency: 3-letter uppercase code echoed on the response (no currency conversion).evaluationTimestamp: ISO-8601Instant.userTier:STANDARDorVIP(available onCart; none of the 6 baseline rules filter on it yet).itemsandpromotions: required, may be empty. Emptypromotionsreturns original line prices and an emptyauditTrail.- Blank
couponCode,paymentMethod, andexclusiveGroupnormalize tonull. - Money fields (
unitPrice,amount,maxDiscountAmount,bundlePrice, tierminSubtotal/amount) are scaled to 2 decimal places on input;percentagestays unscaled (10means 10%). - Category and SKU matches are case-sensitive; coupon and payment matches are case-insensitive.
{
"cartId": "cart-category",
"currency": "INR",
"originalSubtotal": 2500.00,
"totalDiscount": 200.00,
"finalTotal": 2300.00,
"itemizedBreakdown": [
{
"sku": "SKU-SHOES",
"category": "FOOTWEAR",
"quantity": 1,
"unitPrice": 2000.00,
"originalExtendedPrice": 2000.00,
"discount": 200.00,
"finalExtendedPrice": 1800.00
}
],
"auditTrail": [
{
"ruleId": "PROMO_FOOT",
"ruleName": "Footwear 10% off",
"type": "PERCENTAGE_OFF_CATEGORY",
"status": "APPLIED",
"discountAmount": 200.00,
"reason": "Applied: 10% off category FOOTWEAR"
}
]
}itemizedBreakdownpreserves original request order;quantityandunitPriceremain the input values even when units are bundled or free.- Each line's
discountis the sum of all discounts taken off that line across all rules. - Each audit entry's
discountAmountis only what that rule granted (0.00whenSKIPPED). originalSubtotal == sum(originalExtendedPrice)finalTotal == sum(finalExtendedPrice)totalDiscount == originalSubtotal - finalTotal == sum(line.discount) == sum(APPLIED audit.discountAmount)finalExtendedPrice >= 0.00andfinalTotal >= 0.00auditTrailhas one entry per input rule, in evaluation order.
Malformed JSON, unknown enum values (detail: "Malformed request body"), and bean-validation failures (detail: "Request validation failed") return HTTP 400 application/problem+json (already implemented in GlobalExceptionHandler).
PromoEngineFixtureTest posts each file's request and verifies expectedResponse (decimal numeric equality, exact array order and field names, and non-blank auditTrail[].reason):
| Fixture | Scenario |
|---|---|
01_basic_category_percentage.json |
10% off one category; other category unchanged |
02_buy_x_get_y_exact_multiple.json |
Buy 2 get 1 cheapest free on quantity 3 (1 unit free) |
03_bundle_single_complete.json |
One complete bundle with savings split across components |
04_tiered_spend_single_tier.json |
Single spend tier qualifies for fixed discount |
05_coupon_and_payment_method.json |
Matching coupon code and payment method |
06_expired_and_no_matching_rules.json |
Out-of-window rules and non-matching category |
Persistence, authentication, tax, shipping, currency conversion, per-user redemption limits, rejecting duplicate rule IDs, and cross-rule optimization. Quantities fit in a 32-bit int, and all units on a given cart line share one input unitPrice.