BayLearnBayAreaLa8s

Beginner · About 10 minutes · Finance & Payment Systems

The Money Left. But Never Arrived.

Maya sent $30. Her balance fell, but Arjun never received it.

Answers stay in this browser until you save them on this device.

What happened?

Maya has $100 in her account. Arjun has $20.

Maya transfers $30 to Arjun.

The application subtracts $30 from Maya's account—but crashes before adding it to Arjun's account.

Now Maya has $70, Arjun still has $20, and neither knows what happened to the missing $30.

Panel 1

Before

Maya

$100

Arjun

$20

Panel 2

Subtract and save

Maya loses $30 and that change is saved on its own.

Program stops here

Panel 3

After (broken)

Maya

$70

Arjun

$20

Transfer stopped halfway

Maya has $100 and Arjun has $20. After a failed half-transfer, Maya shows $70 but Arjun still has $20 — $30 is missing from the story the accounts tell together.

Your task

How would you make sure the money leaves Maya's account only if it also reaches Arjun's account?

For this challenge

  • Both accounts are stored in the same database.
  • All amounts are US dollars, stored as whole cents in the code.
  • There are no fees, exchange rates, overdrafts, external banks, or payment providers.
  • Each customer request is submitted once. Lost replies and repeated submissions are a later lesson.
  • Use only fictional accounts and local data.
Optional hints
  1. A successful subtraction does not mean the whole transfer is complete.
  2. The database can save a group of changes together.
  3. Check available money while subtracting it, and save a transfer record in the same group.
0/4000
0/4000
0/4000

Related path: System Design Interview Sprint

Interview answer

Explain it in an interview

Maya sent Arjun 30 dollars. Her balance fell and his did not. How do you prevent a half-finished transfer?

Maya transferred 30 dollars to Arjun inside one app and one database. The app subtracted the money from Maya and then crashed before it credited Arjun. Her balance was lower and his was unchanged. I need both balances to move together, or neither of them to move.

How to walk the interviewer through the answer1The debit stuck and the credit did not2Retrying one side can double the harm3Debit, credit, and record together4One connection, then commit or roll back5A lost reply is not a new transfer6Do not claim a bank payment
Walk these steps in order. Each box is one thing to say before you move on.
  1. Step 1

    The debit stuck and the credit did not

    The transfer was two writes in a row: take 30 dollars from Maya, then add 30 dollars to Arjun. The crash landed between them. The database kept the debit because that write had already been saved on its own. Arjun's account never received the credit.

  2. Step 2

    Retrying one side can double the harm

    If I am unsure the debit committed, crediting Arjun can create money, and debiting Maya again can take another 30 dollars. Guessing a refund has the same problem. Two independent updates do not give me one outcome to retry. I need a single commit that covers both accounts.

  3. Step 3

    Debit, credit, and record together

    I debit Maya with a conditional update that refuses a negative balance, credit Arjun, and insert one transfer row in the same transaction. The transfer row exists only when that transaction completes. A failed attempt leaves no transfer row in this table. The original accounts stay as they were when the commit does not happen.

  4. Step 4

    One connection, then commit or roll back

    All three writes use one connection to one SQLite file. An error before commit rolls every change back, so a failed run leaves zero transfer rows. A successful run leaves one row and both balances updated. A check constraint also rejects a negative saved balance.

  5. Step 5

    A lost reply is not a new transfer

    If the process dies before commit, neither balance changes. If it dies after commit, both changes are saved even when Maya never sees the receipt. This lab does not deduplicate retries, so I do not post the transfer again to discover what happened. I read the receipt when I already know its id.

  6. Step 6

    Do not claim a bank payment

    This is an internal transfer in one local database, not a payment across banks and not a full double-entry ledger. Amounts are integer cents, with no fees and no currency conversion. The crash tests cover process exit, not every power failure. The account id in the request is not an authorization check.

Interviewers listen for the boundary between the tempting shortcut and the rule you actually enforce.

Leave these claims out

  • This is a cross-bank payment
  • A lost reply means the transfer failed
  • The transfer table is a complete ledger

I would show that a crash before commit leaves both balances untouched and that a crash after commit leaves both updated. The missing receipt is why I look up the row instead of sending another 30 dollars.

Now say the same idea in your own words, using the story from this challenge.