The Fizz Buzz Multithreaded interview question takes the classic logic problem into the world of concurrency. You have four threads that share a single FizzBuzz object.
fizz() (prints "fizz" if divisible by 3).buzz() (prints "buzz" if divisible by 5).fizzbuzz() (prints "fizzbuzz" if divisible by 15).number() (prints the number if none of the above).
The threads must synchronize so that the output is printed in the correct order from 1 to .Companies like Meta and Amazon use the Fizz Buzz Multithreaded coding problem to evaluate a candidate's knowledge of Concurrency primitives. It tests your ability to use Mutexes, Semaphores, or Condition Variables to coordinate different execution paths. It is a test of avoiding race conditions and deadlocks in a coordinated output stream.
This problem uses the Condition Variable / Barrier pattern.
curr initialized to 1.curr % 3 == 0 and curr % 5 != 0.curr and signals (notifies) all other threads to check the new value.curr > n..
curr = 1. Thread D (number) sees 1 is not div by 3 or 5. Prints "1", curr = 2. Notifies others.curr = 2. Thread D sees 2 is not div by 3 or 5. Prints "2", curr = 3. Notifies others.curr = 3. Thread A (fizz) sees 3 is div by 3. Prints "fizz", curr = 4. Notifies others.while(true) loop without wait() or sleep(), which consumes 100% CPU.if instead of a while when checking the condition before waiting.Master the use of wait() and notifyAll() (or their equivalents in C++ or Python). In a Concurrency interview pattern, always ensure that every path through the code eventually signals the next thread to prevent system hangs.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Building H2O | Medium | Solve | |
| Design Bounded Blocking Queue | Medium | Solve | |
| Print Zero Even Odd | Medium | Solve | |
| The Dining Philosophers | Medium | Solve | |
| Traffic Light Controlled Intersection | Easy | Solve |