Code RoomTransfer locking final balance
HardPrep Room Coding #178

Transfer locking final balance

CodingConcurrencyMid–Senior~30 min

Simulate money transfers between accounts under per-account locking with deadlock avoidance. You start with `balances` (balances[i] is account i's balance). You are given a list of transfers, each [src, dst, amount]. To transfer, BOTH accounts must be lockable; locks are acquired in ascending account-id order (lower id first) to avoid deadlock. A transfer is applied (atomically) only if (a) src != dst and (b) the source has at least `amount` available. If applied, it subtracts amount from src and adds it to dst. Transfers that violate a rule are skipped. Locks are released immediately after each transfer (no held locks across transfers), so the ordering rule never blocks anything here — but you must still reject self-transfers and insufficient-funds transfers. Process transfers in order and return the final balances list.

Implement
apply_transfers(balances: list[int], transfers: list[list[int]]) → list[int]
Examples
in[[100,50],[[0,1,30],[1,0,200]]]out[70,80]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 30 min
InputExpectedGot
[[100,50],[[0,1,30],[1,0,200]]][70,80]not run yetsample