A race occurs when correctness depends on uncontrolled timing between operations. Atomic individual reads and writes do not make a compound transaction atomic.
Before you start
You should understand processes, threads, memory and basic file operations. Track ownership and state changes over time. For concurrent scenarios, write down at least two possible execution orders; the same instructions can behave differently when scheduling or resource availability changes.
The practical goal is to reason through this situation: Two workers read the same balance and overwrite each other’s increments. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: List compound steps
Reading, calculating and writing form more than one atomic action.
Step 2: Construct an interleaving
Two workers can both calculate from the same old value.
Step 3: Protect the invariant
Use an atomic operation or consistent lock around the complete transition.
Worked scenario
Two workers read the same balance and overwrite each other’s increments.
Both workers read balance 10. Each adds one and writes 11, producing 11 instead of 12. Atomic individual reads and writes do not prevent this sequence. A delay can expose the race more frequently in a test, but only changing the coordination establishes correctness.
Common mistake
A delay inserted in tests may expose a race but does not fix it.
Verify the behavior
Trace the lost update and verify the chosen synchronization covers every relevant access.
Interview exercise
Protect a shared update.
Answer and reasoning
Use a synchronization or atomic operation matching the complete invariant, then test concurrent interleavings.
Continue learning
Compare the scenario with the Operating Systems interview questions and test your understanding with the Operating Systems MCQs. For terminology and implementation details, consult the reference material.