CS FundamentalsVery hardAmazon · Microsoft MCQ pattern

Two threads each run the loop `for (i = 0; i < 5; i++) count++;` on a shared int count = 0, with no synchronisation. count++ is compiled to load, add, store. What is the MINIMUM possible final value of count?

  1. A. 5
  2. B. 0
  3. C. 1
  4. D. 2

Correct answer: D. 2

Interleaving: T1 loads 0; T2 runs 4 full iterations (count = 4); T1 stores 1; T2 loads 1; T1 runs its remaining 4 iterations; T2 stores 2. Final = 2. It cannot be lower: the last store comes from some thread's 5th iteration, whose load happens after that thread's own 4th store, so it reads at least 1 and writes at least 2.

More Operating Systems questions

Exams that ask this topic