Problemcounter

Session: Sign in to solve

Solution.txt

Thread-Safe Counter

Problem

Design a Counter that stores one signed integer. One shared instance may be accessed by several threads, and every public method must remain correct when calls overlap.

Requirements

  • Initial state. Each Counter owns an independent value that starts at 0.
  • Increment. increment atomically adds 1 and returns no value.
  • Decrement. decrement atomically subtracts 1 and returns no value. The value may become negative.
  • Read. The read method atomically returns the current value without changing it.
  • Concurrent order. Calls on one shared instance are linearizable: every completed call appears to take effect at one point between its start and finish. This order must preserve call order inside each thread.
  • Progress. A call may wait while another call accesses the counter, but it must not wait for a future call or an external event and must not deadlock.

API

SignatureReturnsBehavior
Counter()Not applicableCreates an independent counter whose value is 0.
increment()No valueAtomically adds 1.
decrement()No valueAtomically subtracts 1, including when the current value is 0 or negative.
Python and C++: get_count()Java: getCount()Signed integerReturns one atomic view of the current value without changing it.

All methods take no arguments and report no domain errors.

Examples

StepOperationResult
1Counter()The counter value is 0.
2increment()The counter value becomes 1.
3increment()The counter value becomes 2.
4ReadReturns 2.
5decrement()The counter value becomes 1.
6ReadReturns 1.

If one thread performs 1,000 increments while another performs 1,000 decrements, a read after both threads finish returns 0.

Constraints

  • 1threads per testcase321 \leq \text{threads per testcase} \leq 32.
  • 1total method calls per testcase1000001 \leq \text{total method calls per testcase} \leq 100000.
  • Every observed value fits in a signed 32-bit integer.
  • Each method must perform O(1)O(1) work, excluding time spent waiting to access shared state.
  • The implementation must not use external storage, network calls, sleeps, or process-wide global state.

Notes

The judge shares one Counter instance among all threads in a testcase. Calls inside one thread keep their listed order. Calls from different threads may interleave in any order that satisfies the linearizability rule.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.