Backend developer interview question · 3–5 years
Two requests read a balance of 100, each subtracts 30, and the balance ends at 70. What happened and what fixes it?
The strongest answer
A lost update: both read then wrote; fix with a single UPDATE that does the arithmetic in the database (SET balance = balance - 30 WHERE balance >= 30), optimistic locking with a version column, or SELECT ... FOR UPDATE
Why it works
Right. Let the database do the arithmetic, or lock or version the row.
What a good answer covers
- Read-modify-write races.
- Atomic UPDATE with the arithmetic.
- Version column for optimistic locking.
- SELECT FOR UPDATE for pessimistic.
Answers that fall short
- Close, but not the strongest: Use a bigger transaction isolation timeout
Timeouts do not stop concurrent reads. - Weak: Retry the second request
Retrying repeats the same race. - Red flag: Read the balance twice
Two reads, same race.
Who gets asked this
- Backend developer
- Full-stack developer
- Data analyst / BI
- Data engineer
- Data science / ML / AI
- Database administrator
More Backend developer questions
- Your queue delivers at least once. What does that force on every consumer?
- A Spring service bean keeps the current user in a field so its helper methods can read it. Under load, users…
- You are designing a Python service that fans out to many slow downstreams. What shapes the design?
- Your service calls three internal APIs and one is intermittently slow. What do you build in?
- You are asked to make a payments service active in two regions. What is the first design question?
- Product wants dashboards over the transactional database. When do you say no and build a separate analytical…
- Design multi-tenancy for a product where some tenants are regulated.
- One endpoint renders PDFs and pegs a core for about two seconds. It is stalling an otherwise healthy Node…