Bird
Raised Fist0

Given the following pseudocode where two threads increment a shared variable count without synchronization:

medium📝 Analysis Q4 of Q15
LLD - Design — Parking Lot System
Given the following pseudocode where two threads increment a shared variable count without synchronization:
Thread 1: temp = count
          temp = temp + 1
          count = temp

Thread 2: temp = count
          temp = temp + 1
          count = temp

If count starts at 0, what is a possible final value after both threads execute once?
A1
B2
C0
D3
Step-by-Step Solution
Solution:
  1. Step 1: Understand race condition

    Both threads read count as 0 before incrementing.
  2. Step 2: Overwrite issue

    Each thread increments locally and writes back, but one write overwrites the other.
  3. Step 3: Possible final values

    Instead of 2, the final count can be 1 due to lost update.
  4. Final Answer:

    1 -> Option A
  5. Quick Check:

    Race conditions cause lost increments [OK]
Quick Trick: Unsynchronized increments can lose updates [OK]
Common Mistakes:
MISTAKES
  • Assuming increments always add up correctly
  • Ignoring the effect of concurrent writes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes