FreeCareerPath
Build the PrimitiveMedium

Thread-Safe Counter

Implement a `ThreadSafeCounter` class with two methods: - `increment()` — increments the counter by 1. - `value()` — returns the current count. Multiple threads will call `increment()` concurrently. The final count after all increments complete must be exactly correct — no lost updates. This is the single most-asked "build the primitive" question in real interviews, because it's the smallest possible example of a race condition: two threads can both read the same value before either writes back the incremented result, silently dropping an increment.

What to expect: A timer starts when you begin. Edit the starter code, run it against the test suite as many times as you like, then finish when you're done. The reference solution and interviewer follow-up questions unlock only after you finish.

Log in to start this session

Timed sessions and your results for "Thread-Safe Counter" are saved to your account — logging in takes a few seconds.