Apdex

Shopping cart coupon engine

Implement applyCoupon for a single cart coupon, following the specified rules and decision order.

Implement a TypeScript function that applies one coupon to a cart at checkout. Put coupon.ts at the submission root and export:

export function applyCoupon(cart: Cart, coupon: Coupon, context: CouponContext): CouponResult;

Do not use external packages, network access, or the file system.

Input and output

interface Cart { subtotal: number }                      // Product subtotal in KRW
interface Coupon {
  id: string;
  type: 'fixed' | 'percent';
  value: number;                                         // KRW for fixed; percentage for percent
  minSubtotal: number;                                   // Minimum order amount in KRW
  expiresAt: string;                                     // ISO 8601 expiration timestamp
}
interface CouponContext { now: string; usedCouponIds: string[] } // ISO 8601 current timestamp
interface CouponResult {
  accepted: boolean;
  discount: number;
  total: number;
  reason: 'ok' | 'invalid' | 'minimum' | 'expired' | 'used';
}

Rules

  1. Validation (invalid) — return reason: 'invalid' if any condition fails:
    • cart.subtotal is a finite nonnegative integer in KRW.
    • coupon.id is a string.
    • coupon.type is 'fixed' or 'percent'; any other value is invalid.
    • For a fixed coupon, coupon.value is a finite nonnegative integer in KRW.
    • For a percent coupon, coupon.value is a finite number from 0 to 100 inclusive; fractions are allowed.
    • coupon.minSubtotal is a finite nonnegative integer in KRW.
    • coupon.expiresAt and context.now are date strings. A date for which Date.parse returns NaN is invalid.
    • context.usedCouponIds is an array of strings.
  2. Minimum (minimum) — reject if cart.subtotal < coupon.minSubtotal. Equality passes.
  3. Expiry (expired) — reject if Date.parse(coupon.expiresAt) <= Date.parse(context.now). Equality means the coupon has expired.
  4. Reuse (used) — reject if context.usedCouponIds contains coupon.id.
  5. Apply (ok) — otherwise apply the coupon:
    • Fixed discount: coupon.value.
    • Percentage discount: Math.floor(cart.subtotal * coupon.value / 100), rounding down to whole KRW.
    • Cap the discount at cart.subtotal; the total cannot be negative.
    • Return { accepted: true, discount, total: cart.subtotal - discount, reason: 'ok' }.

Decision order and rejected results

  • When multiple conditions apply, return only the first reason in this order: invalid → minimum → expired → used.
  • Rejected results use accepted: false and discount: 0.
  • On rejection, total is the original cart.subtotal. If that subtotal itself is invalid under rule 1, use total: 0.
  • Do not mutate cart, coupon, context, or the usedCouponIds array.
  • The same input must always produce the same result. Use only context.now for current time, never Date.now().

Scoring

Seven automatic groups earn 1,000 basis points each: fixed discount, percentage discount, minimum, expiry, reuse, cap, and regression. A group passes only if all of its cases pass. Submitted code runs only in a network-disabled Docker sandbox. Build failure, timeout, or memory exhaustion gives zero automatic points. A human reviewer scores structure, practicality, and depth at 1,000 basis points each. The score is provisional until review.

Submission guide

Submit coupon.ts at the root, or a submission.zip containing coupon.ts at its root. Export applyCoupon(cart, coupon, context). External packages and network access are unavailable.

Scoring criteria

  • Fixed discount: 10% rubric weight
  • Percentage discount and rounding down: 10% rubric weight
  • Minimum order amount, including the boundary: 10% rubric weight
  • Expiry, including the boundary: 10% rubric weight
  • Reject reused coupons: 10% rubric weight
  • Discount cap: 10% rubric weight
  • Validation, decision order, and invariants: 10% rubric weight
  • Structure: 10% rubric weight
  • Practicality: 10% rubric weight
  • Depth: 10% rubric weight

Each criterion is classified as satisfied (at least 80%), tolerating (at least 50%), or unmet, then weighted into a 0–1 rubric index. This is not a measured user satisfaction rate.

Current policy: AI assesses quality points for new submissions. Older challenge wording about human review or provisional scores describes an earlier policy. Each result records the method actually used.

Public resources

  • examples.json

    application/json

    [
      {
        "note": "Apply a fixed coupon",
        "cart": { "subtotal": 30000 },
        "coupon": { "id": "WELCOME3000", "type": "fixed", "value": 3000, "minSubtotal": 20000, "expiresAt": "2026-12-31T23:59:59Z" },
        "context": { "now": "2026-09-23T09:00:00Z", "usedCouponIds": [] },
        "expected": { "accepted": true, "discount": 3000, "total": 27000, "reason": "ok" }
      },
      {
        "note": "Round a percentage discount down to whole KRW",
        "cart": { "subtotal": 12345 },
        "coupon": { "id": "TEN", "type": "percent", "value": 10, "minSubtotal": 0, "expiresAt": "2026-12-31T23:59:59Z" },
        "context": { "now": "2026-09-23T09:00:00Z", "usedCouponIds": [] },
        "expected": { "accepted": true, "discount": 1234, "total": 11111, "reason": "ok" }
      },
      {
        "note": "Below the minimum order amount",
        "cart": { "subtotal": 19999 },
        "coupon": { "id": "WELCOME3000", "type": "fixed", "value": 3000, "minSubtotal": 20000, "expiresAt": "2026-12-31T23:59:59Z" },
        "context": { "now": "2026-09-23T09:00:00Z", "usedCouponIds": [] },
        "expected": { "accepted": false, "discount": 0, "total": 19999, "reason": "minimum" }
      },
      {
        "note": "Expires when the expiry time equals the current time",
        "cart": { "subtotal": 30000 },
        "coupon": { "id": "LAST", "type": "fixed", "value": 1000, "minSubtotal": 0, "expiresAt": "2026-09-23T09:00:00Z" },
        "context": { "now": "2026-09-23T09:00:00Z", "usedCouponIds": [] },
        "expected": { "accepted": false, "discount": 0, "total": 30000, "reason": "expired" }
      },
      {
        "note": "Coupon already used",
        "cart": { "subtotal": 30000 },
        "coupon": { "id": "ONCE", "type": "fixed", "value": 1000, "minSubtotal": 0, "expiresAt": "2026-12-31T23:59:59Z" },
        "context": { "now": "2026-09-23T09:00:00Z", "usedCouponIds": ["ONCE"] },
        "expected": { "accepted": false, "discount": 0, "total": 30000, "reason": "used" }
      },
      {
        "note": "Invalid coupon type",
        "cart": { "subtotal": 30000 },
        "coupon": { "id": "BOGUS", "type": "bogo", "value": 1000, "minSubtotal": 0, "expiresAt": "2026-12-31T23:59:59Z" },
        "context": { "now": "2026-09-23T09:00:00Z", "usedCouponIds": [] },
        "expected": { "accepted": false, "discount": 0, "total": 30000, "reason": "invalid" }
      }
    ]
    

Starter files

  • coupon.ts

    export interface Cart {
      subtotal: number;
    }
    
    export interface Coupon {
      id: string;
      type: 'fixed' | 'percent';
      value: number;
      minSubtotal: number;
      expiresAt: string;
    }
    
    export interface CouponContext {
      now: string;
      usedCouponIds: string[];
    }
    
    export type CouponReason = 'ok' | 'invalid' | 'minimum' | 'expired' | 'used';
    
    export interface CouponResult {
      accepted: boolean;
      discount: number;
      total: number;
      reason: CouponReason;
    }
    
    export function applyCoupon(cart: Cart, coupon: Coupon, context: CouponContext): CouponResult {
      // TODO: Implement the rules in decision order (invalid → minimum → expired → used → ok).
      void cart;
      void coupon;
      void context;
      throw new Error('TODO: applyCoupon is not implemented yet');
    }
    

Related practical guides

Sign in to submit How Apdex works