Fizz Buzz Multithreaded - Problem

Welcome to the world of multithreaded programming! This problem combines the classic FizzBuzz game with concurrent execution, creating a fascinating challenge in thread synchronization.

You're given a FizzBuzz class that needs to coordinate four different threads to produce the correct output sequence. Each thread has a specific responsibility:

  • Thread A: Calls fizz() to print "fizz" when numbers are divisible by 3 but not 5
  • Thread B: Calls buzz() to print "buzz" when numbers are divisible by 5 but not 3
  • Thread C: Calls fizzbuzz() to print "fizzbuzz" when numbers are divisible by both 3 and 5
  • Thread D: Calls number() to print the actual number when it's not divisible by 3 or 5

The challenge is to synchronize these threads so they output the sequence [1, 2, "fizz", 4, "buzz", "fizz", 7, 8, "fizz", "buzz", 11, "fizz", 13, 14, "fizzbuzz", ...] in the correct order, despite running concurrently.

Goal: Implement thread synchronization mechanisms to ensure the output appears in sequential order from 1 to n.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 15
โ€บ Output: 1,2,fizz,4,buzz,fizz,7,8,fizz,buzz,11,fizz,13,14,fizzbuzz
๐Ÿ’ก Note: For n=15, the sequence includes all FizzBuzz rules. Numbers 1,2,4,7,8,11,13,14 are printed as-is, multiples of 3 (but not 5) become 'fizz', multiples of 5 (but not 3) become 'buzz', and multiples of both become 'fizzbuzz'.
example_2.py โ€” Small Case
$ Input: n = 5
โ€บ Output: 1,2,fizz,4,buzz
๐Ÿ’ก Note: For n=5, we get: 1 (not divisible), 2 (not divisible), 3 (divisible by 3 only โ†’ fizz), 4 (not divisible), 5 (divisible by 5 only โ†’ buzz).
example_3.py โ€” Edge Case
$ Input: n = 1
โ€บ Output: 1
๐Ÿ’ก Note: For n=1, only the number 1 is in range. Since 1 is not divisible by 3 or 5, it's printed as-is by the number thread.

Visualization

Tap to expand
Multithreaded FizzBuzz OrchestraFIZZViolinistBUZZTrumpeterFIZZBUZZPianistNUMBERDrummerCONDUCTORBeat: 3Shared CounterMusical Score: 1, 2, "fizz", 4, "buzz", "fizz", 7, 8, "fizz", "buzz"...Each musician plays only when the beat matches their rule:Fizz: 3,6,9,12... | Buzz: 5,10,20,25... | FizzBuzz: 15,30,45... | Number: 1,2,4,7,8...๐ŸŽผ Perfect harmony through condition variable synchronization!ACTIVEwaitingwaitingwaiting
Understanding the Visualization
1
Orchestra Assembly
Four musicians (threads) take their positions: Fizz violinist, Buzz trumpeter, FizzBuzz pianist, and Number drummer. Each knows exactly when to play.
2
Conductor's Baton
The shared counter acts as the conductor's baton, indicating which beat (number) we're currently on. All musicians watch the conductor.
3
Wait for Cue
Musicians wait silently (condition variable wait) until the conductor signals it's their turn. No energy wasted on constant checking.
4
Perfect Harmony
When it's their turn, the appropriate musician plays their note, the conductor moves to the next beat, and wakes everyone for the next cue.
Key Takeaway
๐ŸŽฏ Key Insight: Use condition variables to make threads wait efficiently without consuming CPU cycles, then wake them up only when the shared state changes - creating perfect synchronization like a well-conducted orchestra!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each number is processed exactly once with efficient waiting

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Uses only synchronization primitives and a counter

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 50
  • Four threads will be created to call the four functions concurrently
  • The same instance of FizzBuzz will be passed to all four threads
  • Thread safety is required - output must be in correct sequential order
Asked in
Amazon 35 Microsoft 28 Google 22 Meta 18
42.8K Views
Medium Frequency
~25 min Avg. Time
1.5K 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