This question evaluates a candidate's mastery of concurrent programming and data-integrity principles, focusing on thread safety, atomic state updates, and deadlock avoidance when transferring funds between shared accounts.
You are given an in-memory store of bank accounts:
accountId
and a
balance
(integer cents).
transfer(fromId, toId, amount)
concurrently.
Implement transfer(fromId, toId, amount) such that it is:
fromId
to
toId
entirely, or nothing changes.
amount > 0
Provide the API design and implementation approach, including how you prevent deadlocks and ensure correctness.