Code Room
CodingHardcod-g1019
Subject Transfer locking final balanceLevel Mid–Senior~30 minCommon in Concurrency interviewsIndustries Software development, Technology

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.