Code Room
Code reviewHardcr-g578
Subject Deadlock lock orderingLevel Senior–Staff~32 minCommon in Concurrency interviewsIndustries Software development, Technology

Question

Review this Java account-transfer method.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.

Talk through your review
Code to reviewjava
class Account {    private long balance;    final Object lock = new Object();    long balance() { synchronized (lock) { return balance; } }    void credit(long a) { synchronized (lock) { balance += a; } }    void debit(long a)  { synchronized (lock) { balance -= a; } }} void transfer(Account from, Account to, long amount) {    synchronized (from.lock) {        synchronized (to.lock) {            from.debit(amount);            to.credit(amount);        }    }}
Run or narrate your approach, then ask the coach.