DEV Community

Cover image for #10daysofcodechallenge by Ingressive for Good (#Day 6)
Dennar David
Dennar David

Posted on

#10daysofcodechallenge by Ingressive for Good (#Day 6)

I've just begun a 10-day coding challenge organized by Ingressive for Good and I'll be sharing my thoughts on Day 6 of the challenge.

Let me briefly describe how the challenge works before I share my experience with you. From September 21 through October 30, 2022, the challenge is to solve one common algorithm problem every day for ten days.

Today's algorithm problem was "Fizz Buzz Multithreaded." It is the 1195th algorithm challenge on "Leetcode.com".
Here is the question:

You have the four functions:

printFizz that prints the word "fizz" to the console,
printBuzz that prints the word "buzz" to the console,
printFizzBuzz that prints the word "fizzbuzz" to the console, and
printNumber that prints a given integer to the console.
You are given an instance of the class FizzBuzz that has four functions: fizz, buzz, fizzbuzz and number. The same instance of > FizzBuzz will be passed to four different threads:

Thread A: calls fizz() that should output the word "fizz".
Thread B: calls buzz() that should output the word "buzz".
Thread C: calls fizzbuzz() that should output the word "fizzbuzz".
Thread D: calls number() that should only output the integers.
Modify the given class to output the series [1, 2, "fizz", 4, >"buzz", ...] where the ith token (1-indexed) of the series 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, or

i if i is not divisible by 3 or 5.

I'll now discuss my algorithm solving experience. Since the challenge began, this algorithm has proven to be the most challenging. I used the Python threading module for this task (The problem required multi threading). I added some conditionals to a function to determine when to utilize the acquire() and release() methods on a particular instance. Then, each thread of the "fizzbuzz" made a call to this function.

This task was completed in Python, took 34 milliseconds to execute, and consumed about 13.6 MB of memory.

Top comments (0)