KatInterview

Design a Safe, Idempotent Auto-Approval Engine for Order Refunds

Data Structure DesignModel answer
MediumModel answerData Structure Designdomain-modelingidempotencyrules-engine

Last updated

Design and code a refund decision engine that applies pluggable rules, never refunds more than was paid, and handles retried or concurrent requests without paying twice.

Scenario

An e-commerce company wants refund requests against existing orders to be handled automatically, with no human involved in the normal case. For each request the system must decide:

  1. Whether to refund.
  2. How much to refund.
  3. Why it reached that decision.

It must then record the outcome so that the same money is never paid out twice.

You will work through this in a live, collaborative coding session of about 75 minutes. That includes setting up the project and a closing discussion. The requirements arrive in small increments, and finishing all of them is not expected. What matters is how you model the problem, which edge cases you raise, and how clearly you explain your choices while you write working code.

Given

  • A refund request refers to an order and states what the customer wants back.
  • Which conditions make a request automatically eligible is open. Part of your job is to ask about and settle them (order status, time since purchase or delivery, requested amount, stated reason, customer refund history, and so on).
  • Two rules hold for every version of the system:
    • Cap invariant: the total refunded for an order must never exceed the amount paid for it.
    • Retry safety: submitting the same request again must not produce a second refund.

What to deliver

  • A small domain model and a public API, for example decide(request) -> Decision and a way to record or execute the result. An in-memory implementation is fine unless you agree otherwise.
  • Eligibility logic that can grow as the requirements are extended.
  • Every decision must carry an outcome (approved, rejected, or sent to manual review), an amount and a reason.
  • A few quick tests for the rules and for the two invariants.
  • A short verbal explanation of your trade-offs, including how you would handle concurrent requests for one order and a failing payment call.

State your assumptions before coding. Questions worth asking include:

  • Are partial refunds allowed, by amount or by line item?
  • Do shipping and tax come back with the items?
  • Is a request that does not qualify rejected, or queued for review?
  • Is the payout immediate or deferred?
  • How is a retry identified?

Hints

Hint 1

Keep money out of floating point: use integer minor units (cents) or a decimal type, and compute the refundable amount as paid minus the sum of already-refunded amounts. Put this check in one function that every path goes through.

Hint 2

Model each eligibility rule as a small object or function that takes the request and order context and returns pass, fail or review plus a reason. A decision engine then runs the list in order, so a new requirement means adding a rule rather than editing existing ones.

Hint 3

For retries, require a client-supplied idempotency key and store the first result under it. Return that stored result for any repeat. For concurrency, make the 'check remaining balance, then write refund' step atomic per order, using a lock or a conditional database update.