Beginner · About 10 minutes · Finance & Payment Systems
The Refund Was Bigger Than the Payment
Maya paid $100. After $60 was refunded, another agent requested $50.
Answers stay in this browser until you save them on this device.
What happened?
Maya pays $100 for an order.
The store refunds $60 for one returned item.
Later, another support agent requests a $50 refund.
The application checks only that $50 is less than the original $100 payment—and approves it.
Maya has now received $110 back on a $100 payment.
Your task
How would you prevent a refund from exceeding the amount still available to return?
For this exercise
- One completed $100 payment can have several partial refunds.
- Total refunds cannot exceed the original payment.
- There are no fees, currency conversions, canceled refunds or payment reversals.
- Different agents may submit separate requests at the same time.
- Each request is submitted once. Repeated submissions and lost replies are a later lesson.
- The downloadable lab only records fictional refunds locally; no money moves.
Optional hints
- The original payment is not the amount still available to refund.
- Subtract earlier refunds from the original payment.
- Ask the database to check the remaining amount while saving the new total.
Related path: System Design Interview Sprint
Interview answer
Explain it in an interview
Maya paid 100 dollars, already got 60 dollars back, and another agent asked for 50. How do you stop the extra refund?
Maya paid 100 dollars. A first refund of 60 dollars was recorded, so 40 dollars remained. A support agent then requested 50 dollars. The app compared 50 dollars with the original 100 dollars and approved it, which is more than the 40 dollars still refundable.
Step 1
The second refund ignored the remainder
After the 60 dollar refund, the payment still showed its original 100 dollars, and that is the number the check used. Fifty dollars is less than 100, so the request passed. The books would then show 110 dollars of refunds against a 100 dollar payment. The remainder was never part of the decision.
Step 2
The original payment is the wrong ceiling
The original amount is history, and it does not shrink when a refund is recorded. Checking every new request against 100 dollars keeps approving slices that no longer fit. Storing a leftover balance that can drift away from the refund rows is another way to approve too much. The ceiling has to be what is still unrefunded.
Step 3
Approve only what is still refundable
Remaining is computed as the original amount minus refunds already recorded, so it is 40 dollars in this story. I increase the refunded total only when the new request fits that remainder. The 50 dollar request is rejected and leaves no refund row. A later request for the remaining 40 dollars can still succeed.
Step 4
Update the total and the row together
The conditional update and the refund insert share one transaction, and I confirm only after commit. If the save fails, both changes roll back. The original payment amount stays unchanged. The schema alone cannot keep the total equal to the refund rows if someone edits the tables outside this transaction.
Step 5
Do not resubmit a refund you cannot see
There is no request deduplication. Repeating a 10 dollar refund can record 20 dollars when enough remains. After a lost reply I do not submit again, because the refund row may already exist. A lock-wait error means the database was busy, not that the amount was too large.
Step 6
A recorded refund did not move money
These rows are simulated refunds. Success means the record committed, not that a payment provider sent money. A database rollback cannot undo an external refund, and this lab never calls a provider. There is no authentication, no currency conversion, and the database is one local file.
Do not stop here
I would approve the 50 dollar refund because it is less than the 100 dollars Maya originally paid.
Say this boundary
I reject 50 dollars because only 40 dollars remain, and I record a refund only when that request fits inside the same transaction.
Leave these claims out
- Compare each refund with the original payment
- A successful row means the provider paid
- Repeating the post is a safe retry
I would put the 60 dollar refund on the table first, then show why 50 fails and why 40 can still pass. The boundary is the remaining amount, committed together with the refund row.
Now say the same idea in your own words, using the story from this challenge.