Code Room
Code reviewMediumcr-g228
Subject DeadlocksLevel Mid–Senior~25 minCommon in Concurrency interviewsIndustries Software development

Question

Review this Java account-transfer routine.

What is the concurrency hazard, and how is it triggered?

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 {    final Object lock = new Object();    long balance;} void transfer(Account from, Account to, long amt) {    synchronized (from.lock) {        synchronized (to.lock) {        // (1)            from.balance -= amt;            to.balance   += amt;        }    }}
Run or narrate your approach, then ask the coach.