Sobes.tech
Senior

We brought 20 meat grinders, shipped 5 — how many are left in the warehouse and what is the current system status?

sobes.tech AI

Answer from AI

If 20 meat grinders were delivered and 5 were shipped out, then 15 meat grinders remain in stock.

The current state in the system should reflect this quantity — for example, in the database or in the application's memory, the value should be 15 for available stock.

It is important to ensure the atomicity of the state update operation to avoid desynchronization. For example, if shipping and receiving happen in parallel, synchronization mechanisms or transactions should be used to ensure the final count is correct.

An example in Go using a mutex to protect the state:

var (
    stock = 20
    mu    sync.Mutex
)

func ship(amount int) {
    mu.Lock()
    defer mu.Unlock()
    if stock >= amount {
        stock -= amount
    } else {
        // handle error: insufficient stock
    }
}

func main() {
    ship(5)
    fmt.Println("Remaining stock:", stock) // 15
}

Thus, the system's state is the current quantity of goods, reflecting all receipt and expenditure operations.

We brought 20 meat grinders, shipped 5 — how many are… - sobes.tech