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 5 test cases (2 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 →```
class FizzBuzz:
n: int
cur: int = 1
lock: Mutex
cv: ConditionVariable
def role(self, predicate, emit):
while True:
with self.lock:
while self.cur <= self.n and not predicate(self.cur):
self.cv.wait(self.lock)
if self.cur > self.n:
self.cv.notify_all()
return
emit(self.cur)
self.cur += 1
self.cv.notify_all()
fizz(): self.role(lambda x: x % 3 == 0 and x % 5 != 0, lambda x: print("fizz"))
buzz(): self.role(lambda x: x % 5 == 0 and x % 3 != 0, lambda x: print("buzz"))
fizzbuzz(): self.role(lambda x: x % 15 == 0, lambda x: print("fizzbuzz"))
number(): self.role(lambda x: x % 3 != 0 and x % 5 != 0, lambda x: print(x))
```
Each thread waits until its predicate matches the current counter, then prints and advances. The notify_all on advance wakes whichever role is responsible next.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
These apply to the pattern as a whole, not just this problem.