Concurrency & State
Code Review: Race Condition & State Mutability
hardCode ReviewJS / TS
Problem Statement
Review the following wallet balance transfer handler. Identify how concurrent operations produce inconsistent state, explain the race condition window, and refactor using atomic operations or database transactions.
Code under review
JAVASCRIPT• handler.js
Pairlet Snippet
// Account balance transfer service
async function transferFunds(senderId, receiverId, amount) {
const sender = await db.findUser(senderId);
if (sender.balance < amount) {
throw new Error("Insufficient funds");
}
await new Promise(resolve => setTimeout(resolve, 50));
const receiver = await db.findUser(receiverId);
const newSenderBalance = sender.balance - amount;
const newReceiverBalance = receiver.balance + amount;
await db.updateUserBalance(senderId, newSenderBalance);
await db.updateUserBalance(receiverId, newReceiverBalance);
return { success: true };
}
Read only. Write down what you would flag below, then compare with the reference notes.
JavaScript & TypeScript versions included
Run this as a live interviewPractice solo
Write down what you would flag in this code, then compare it with the reference notes.
Write your findings first, then check yourself against the notes.