KatInterview

Fixing a Lost-Update Race in a Deposit Method You Cannot Edit

Data Structure DesignModel answer
MediumModel answerData Structure Designconcurrencyrace-conditionmutex

Last updated

Diagnose the race in an account deposit that reads, pauses, then writes, and add locking through a subclass, wrapper or embedding in Python and Go without touching the original type.

You are given an account class that several threads share. Its deposit method reads the balance into a local variable, pauses to simulate processing, and then writes back the local value plus the amount.

import time

class BankAccount:
    def __init__(self, balance=0):
        self.balance = balance

    def deposit(self, amount):
        current = self.balance          # read
        time.sleep(0.1)                 # simulated processing delay
        self.balance = current + amount # write

Multiple threads in one process call deposit on the same object at the same time. Diagnose the problem, fix it, and then repeat the fix in Go.

Rules

  • BankAccount must not be modified. Pasting its body into a new class and editing that copy is not allowed. The safe version has to call the existing deposit through a subclass or a wrapper.
  • Treat the time.sleep(0.1) as part of the method you cannot change. It stands in for real work between the read and the write.
  • All threads live in one process and share one account object.

Part 1: Find the bugs. List every concurrency defect in deposit and name the root cause. Walk through a concrete two-thread run starting at balance 0 that ends with a wrong balance, giving the expected and actual results. Say whether removing the delay would make the method correct.

Part 2: Fix it in Python. Write the thread-safe version under the rules above. Show where the lock lives, when it is created, and which statements it protects. Explain how callers should read the balance and how you would write a test that fails reliably before the fix and passes after it.

Part 3: Fix it in Go. Here is the same account in Go:

type BankAccount struct {
    Balance int
}

func (a *BankAccount) Deposit(amount int) {
    current := a.Balance
    time.Sleep(100 * time.Millisecond)
    a.Balance = current + amount
}

Go has no inheritance. Build a thread-safe equivalent without editing BankAccount, and explain what can still go wrong silently.

State any assumptions you make, for example whether other methods must be safe, whether one lock per account is expected, and whether anything beyond threads in one process is involved.

Hints

Hint 1

Ask what the method does as a sequence of steps: a read, some work, then a write that depends on the earlier read. What happens if another thread runs between those steps?

Hint 2

The lock must be held across the whole read-delay-write sequence, so wrap the call to the inherited method instead of locking inside separate pieces. Create one lock per account in the constructor, not per call and not at module level by accident.

Hint 3

In Go, embed the original struct in a new struct together with a sync.Mutex, and define a Deposit on the wrapper that locks, defers the unlock, and calls the embedded method. Think about pointer versus value receivers, copying a mutex, and callers who can still reach the inner struct directly.