AlgoMaster Logo
AlgoMasterFizz Buzz Multithreadedmedium

Fizz Buzz Multithreaded

medium

Four threads share one FizzBuzz instance and produce the Fizz Buzz sequence from 1 through n:

  • Thread A calls fizz(printFizz) and must output "fizz" for numbers divisible by 3 but not 5.
  • Thread B calls buzz(printBuzz) and must output "buzz" for numbers divisible by 5 but not 3.
  • Thread C calls fizzbuzz(printFizzBuzz) and must output "fizzbuzz" for numbers divisible by both 3 and 5.
  • Thread D calls number(printNumber) and must output every remaining number.

The supplied callbacks perform the output. Your task is to synchronize the four methods so the callbacks are invoked in the correct order and every value from 1 through n is produced exactly once.

The judge creates the shared object, starts all four threads, and calls each method once. Your implementation should coordinate those calls rather than create threads itself.

The judge also preloads the standard concurrency and callback APIs for each supported language. You do not need to add import, include, or using statements.

Example 1:

Input:

Output:

Example 2:

Input:

Output:

Constraints

  • 1 <= n <= 50
  • The four methods are called concurrently on the same FizzBuzz instance.
  • Each supplied callback is thread-safe and must be called only for the value assigned to that method.
Loading...

Input

n = 15

Output

[1, 2, "fizz", 4, "buzz", "fizz", 7, 8, "fizz", "buzz", 11, "fizz", 13, 14, "fizzbuzz"]

Run is a quick check against the first couple of scenarios, which is roughly what these examples describe. Submit puts your class under the full set, which stays hidden.