Fizz Buzz Multithreaded - Problem

You have four functions: printFizz that prints "fizz", printBuzz that prints "buzz", printFizzBuzz that prints "fizzbuzz", and printNumber that prints a given integer.

You are given an instance of the class FizzBuzz that has four functions: fizz, buzz, fizzbuzz and number. The same instance will be passed to four different threads:

  • Thread A: calls fizz() that should output "fizz"
  • Thread B: calls buzz() that should output "buzz"
  • Thread C: calls fizzbuzz() that should output "fizzbuzz"
  • Thread D: calls number() that should output integers

Modify the given class to output the series [1, 2, "fizz", 4, "buzz", ...] where the i-th token (1-indexed) is:

  • "fizzbuzz" if i is divisible by 3 and 5
  • "fizz" if i is divisible by 3 and not 5
  • "buzz" if i is divisible by 5 and not 3
  • i if i is not divisible by 3 or 5

Input & Output

Example 1 — Basic Case
$ Input: n = 15
Output: ["1", "2", "fizz", "4", "buzz", "fizz", "7", "8", "fizz", "buzz", "11", "fizz", "13", "14", "fizzbuzz"]
💡 Note: For each number 1 to 15: 1,2 → numbers; 3,6,9,12 → fizz; 5,10 → buzz; 15 → fizzbuzz
Example 2 — Small Range
$ Input: n = 5
Output: ["1", "2", "fizz", "4", "buzz"]
💡 Note: 1,2,4 are regular numbers; 3 is divisible by 3 only → fizz; 5 is divisible by 5 only → buzz
Example 3 — Include FizzBuzz
$ Input: n = 30
Output: ["1", "2", "fizz", "4", "buzz", "fizz", "7", "8", "fizz", "buzz", "11", "fizz", "13", "14", "fizzbuzz", "16", "17", "fizz", "19", "buzz", "fizz", "22", "23", "fizz", "buzz", "26", "fizz", "28", "29", "fizzbuzz"]
💡 Note: Numbers 15 and 30 are divisible by both 3 and 5, so they become fizzbuzz

Constraints

  • 1 ≤ n ≤ 50

Visualization

Tap to expand
Fizz Buzz Multithreaded INPUT 4 Concurrent Threads Thread A fizz() Thread B buzz() Thread C fizzbuzz() Thread D number() Input Parameter: n = 15 Rules: i % 3 == 0 AND i % 5 == 0 fizzbuzz i % 3 == 0 only fizz i % 5 == 0 only buzz otherwise i Shared Counter: current = 1 ALGORITHM STEPS Condition Variable Sync 1 Initialize Sync mutex + condition_variable current = 1, n = 15 2 Thread Wait Loop Each thread waits for its condition to be true 3 Check Divisibility Wake correct thread based on current % 3,5 4 Print and Notify Print value, current++ notify_all() threads Condition Variable Flow wait() print notify Loop until current > n FINAL RESULT Output Sequence (n=15): 1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 15 (3x5) Thread Execution Count fizz: 4 times buzz: 2 times fizzbuzz: 1 time number: 8 times Total: 15 outputs in order OK - Synchronized! All threads terminated safely Key Insight: Condition variables allow threads to wait efficiently without busy-waiting. Each thread checks its specific condition (divisibility by 3, 5, both, or neither) and only wakes when current value matches. notify_all() ensures all threads re-check conditions after each increment, maintaining correct order. TutorialsPoint - Fizz Buzz Multithreaded | Condition Variable Synchronization
Asked in
Google 25 Facebook 20 Microsoft 18 Amazon 15
28.0K Views
Medium Frequency
~30 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen