Code RoomTransfer succeeds with negative balance
HardPrep Room Coding #1950

Transfer succeeds with negative balance

Code reviewDatabases & SQLReliability & on-callSenior–Staff~22 min

Review this Go funds-transfer. It opens a transaction, debits, validates, then credits.

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.

0:00 of about 22 min
Mark a line and say what kind of problem it is.0 findings
1func transfer(db *sql.DB, from, to string, amt int) error {
2 tx, _ := db.Begin()
3 tx.Exec("UPDATE acct SET bal = bal - $1 WHERE id = $2", amt, from)
4 var bal int
5 tx.QueryRow("SELECT bal FROM acct WHERE id = $1", from).Scan(&bal)
6 if bal < 0 {
7 return errors.New("insufficient funds")
8 }
9 tx.Exec("UPDATE acct SET bal = bal + $1 WHERE id = $2", amt, to)
10 return tx.Commit()
11}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.