LeetCode 1195: Fizz Buzz Multithreaded Solution

Master LeetCode problem 1195 (Fizz Buzz Multithreaded), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1195. Fizz Buzz Multithreaded

Problem Explanation

Explanation:

To solve this problem, we can create four separate functions in the FizzBuzz class: fizz, buzz, fizzbuzz, and number. Each function will be responsible for printing the appropriate output based on the current index.

We will use a shared variable count to keep track of the current index that each thread will use to determine which output to print. We will also use a mutex to synchronize access to the count variable among the threads.

For each number from 1 to n, we will check if the number is divisible by 3 and 5, by 3 only, by 5 only, or neither. Based on this check, we will call the appropriate function to print the output. :

Solution Code

import java.util.concurrent.Semaphore;

class FizzBuzz {
    private int n;
    private int count;
    private Semaphore fizzSem, buzzSem, fizzBuzzSem, numberSem;

    public FizzBuzz(int n) {
        this.n = n;
        this.count = 1;
        this.fizzSem = new Semaphore(0);
        this.buzzSem = new Semaphore(0);
        this.fizzBuzzSem = new Semaphore(0);
        this.numberSem = new Semaphore(1); // Allow number to be printed first
    }

    public void fizz(Runnable printFizz) throws InterruptedException {
        while (count <= n) {
            fizzSem.acquire();
            if (count > n) break;
            printFizz.run();
            releaseSemaphore();
        }
    }

    public void buzz(Runnable printBuzz) throws InterruptedException {
        while (count <= n) {
            buzzSem.acquire();
            if (count > n) break;
            printBuzz.run();
            releaseSemaphore();
        }
    }

    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        while (count <= n) {
            fizzBuzzSem.acquire();
            if (count > n) break;
            printFizzBuzz.run();
            releaseSemaphore();
        }
    }

    public void number(Runnable printNumber) throws InterruptedException {
        while (count <= n) {
            numberSem.acquire();
            if (count % 3 != 0 && count % 5 != 0) {
                printNumber.run();
            }
            releaseSemaphore();
        }
    }

    private void releaseSemaphore() {
        count++;
        if (count > n) {
            fizzSem.release();
            buzzSem.release();
            fizzBuzzSem.release();
        } else {
            if (count % 3 == 0 && count % 5 == 0) {
                fizzBuzzSem.release();
            } else if (count % 3 == 0) {
                fizzSem.release();
            } else if (count % 5 == 0) {
                buzzSem.release();
            } else {
                numberSem.release();
            }
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1195 (Fizz Buzz Multithreaded)?

This page provides optimized solutions for LeetCode problem 1195 (Fizz Buzz Multithreaded) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1195 (Fizz Buzz Multithreaded)?

The time complexity for LeetCode 1195 (Fizz Buzz Multithreaded) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1195 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1195 in Java, C++, or Python.

Back to LeetCode Solutions