We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A hard concurrency problem, graded against 6 test cases (3 of them hidden).
Coordinating threads with locks, semaphores, and condition variables.
Reach for it when you see: Ordering guarantees between threads, bounded buffers, or classic synchronisation puzzles.
More Concurrencyproblems →```
hSem = Semaphore(2) # at most 2 H past the gate at once
oSem = Semaphore(1) # at most 1 O past the gate
barrier = CyclicBarrier(3)
hydrogen():
hSem.acquire()
barrier.await() # the H prints inside this section
print('H')
hSem.release()
oxygen():
oSem.acquire()
barrier.await()
print('O')
oSem.release()
```
The barrier guarantees three threads meet before any of them prints; the per-element semaphores cap how many of each species can sit at the barrier so the next molecule can't start forming until the previous one releases.
Simulation logic for the grader
Walk the arrival string in order, accumulating counts. Whenever `countH >= 2` and `countO >= 1`, emit "HHO" and decrement `countH -= 2; countO -= 1`. The leftover at the end represents threads still blocked at the barrier.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.