Do you think he clicks?

Exclusive Hard 5 Concurrency Optiver

Implement the class Autoclicker, which automatically injects inputs into your computer while holding a designated macro key.

For obvious reasons, instead of actually injecting input into your computer like a real autoclicker would, you will use provided API functions for grading.

This question uses a custom judge to determine correctness. Therefore, you do not need to write any input/output parsing.


Provided Custom API

The judge will provide you with three thread-safe callable API functions:

listener()

  • Returns the current key state as a compact command string.
  • The return value is one of:
    • "" (empty string): no relevant key is currently held
    • "h": the macro key is held (no modifiers)
    • "shift+h": macro key is held with Shift
    • "ctrl+h": macro key is held with Ctrl
    • "alt+h": macro key is held with Alt
    • "shift+ctrl+h" (and other combinations): macro key held with multiple modifiers
  • This function is non-blocking and may be called frequently.
  • The judge may rapidly change the returned value (simulating key press/release jitter).

click()

  • Injects a single “click” event into the judge’s simulated system.
  • The judge records timestamps of clicks to validate timing and correctness.

sleep_ms(ms)

  • Suspends the calling thread for approximately ms milliseconds.
  • Guarantees that:
    • The calling thread will not consume CPU while sleeping.
    • Other threads may continue executing during this time.
  • May return slightly earlier or later than ms; your implementation must tolerate small timing variance.

Important

  • You must use sleep_ms instead of any standard sleep function (e.g. std::this_thread::sleep_for, usleep, nanosleep, etc.).
  • Behavior is undefined if ms is negative.

Meaning of commands

Your autoclicker should treat the following as:

  • Macro held: listener() returns a string whose final token is "h" (examples: "h", "shift+h", "ctrl+h", "shift+ctrl+h").
  • Macro not held: listener() returns "" or anything not ending in "h".

You must not assume the exact set of modifier combinations; just parse the string robustly.


Constructor

Autoclicker(min_delay_ms, max_delay_ms, seed = 1)

  • min_delay_ms — minimum delay (in milliseconds) between consecutive clicks while the macro is held.
  • max_delay_ms — maximum delay (in milliseconds) between consecutive clicks while the macro is held.
  • seed — seed for your RNG. Using the provided seed ensures deterministic behavior for testing.

Methods

start()

  • Starts the autoclicker’s internal threads.
  • After start() is called, your autoclicker must begin reacting to listener().
  • While the macro key is held (listener() ends with "h"), it should:
    • Call click()
    • Then wait a random delay uniformly chosen in [min_delay_ms, max_delay_ms] (inclusive)
    • Repeat, as long as the macro remains held
  • When the macro becomes not held, clicking must stop promptly.

stop()

  • Stops the autoclicker and joins all internal threads.
  • After stop() returns:
    • No further calls to click() may occur.
    • All background work must be terminated cleanly.

Required Concurrency Design

To pass the judge, your solution must follow these rules:

  • listener() must be polled from a dedicated listener thread.
  • click() must not be called from the listener thread.
  • Clicking logic must run on a separate worker thread that:
    • Waits until the macro becomes held
    • Generates random delays
    • Calls click() at the correct cadence
    • Stops quickly when the macro is released or when stop() is called
  • Your implementation must be free of data races and undefined behavior.

Notes

  • Release responsiveness matters: if the macro is released, the judge expects clicking to stop quickly (small timing tolerance allowed).
  • stop() may be called at any time, including while the worker is sleeping between clicks.
  • The judge will test:
    • rapid toggling of the macro key
    • modifier noise (different strings ending with "h")
    • correct random interval bounds
    • clean shutdown (no leaked or detached threads)
  • Random interval requirement: delays must be random, not constant or periodic. The judge checks for non-trivial variance over long holds.
  • The judge controls time progression internally. Using the provided sleep_ms function is required to ensure deterministic behavior across platforms.
Accepted 1/1
Acceptance 100%
Loading editor...
Sample Input:
Running custom tests...
Expected Output: