Digging Deeper Into New Territory (Leetcode Problems) - #Day 6

Fizz Buzz Multithreaded Problem (I4G 10 Days Of Code Challenge)

ยท

3 min read

So far, most of the challenges that I have faced from the previous days have been tagged as easy (Although some of them never felt like it). Today's challenge, however, was not tagged as easy and to be honest, it wasn't.

The Challenge

6_question.png From the prompt, you can see the words threads which means that today's challenge shares similarity to Day 4's challenge.

Problem Approach

Well, as stated previously, the challenge was based on a concept which I had explored in Day 4. But, today's challenge gave me the same feeling as Day 1(maybe because of the difficulty level).

Although, the idea of the program was simple, it had been complicated because it used the concept of threads when it could have simply used a loop with conditional statements (๐Ÿ˜ง, Leetcode just knows how to make things hard, don't they)

Getting into the problem, I was first bugged with the question, Where do I even start ?. So I used the previous approach of just going there and working with the already existing boilerplate.

From there, I jumped into the challenge using the only thing I knew how to use with this rather novel concept of threading, mutexes.

The idea, I had in mind was this:

  1. Create mutex(es) to be accessible to the functions fizz, buzz, fizzbuzz and number
  2. Using one or more mutexes, lock the methods and run them based on the required conditions
  3. Unlock the methods once done

On using this approach, I quickly ran into a wall.

A Brief Outlook on Mutexes

Mutexes are used in threading to allow only one thread to run at a time which means, they are used as a guard or receptionist, to allow only one thread to utilize a method(s) at a point in time.

The prompt, however, requires all the methods to run concurrently but at the same time in sync with each other. In better words, the challenge wants the functions fizz, buzz, fizzbuzz and number to basically interact with each other as if they were one thread.

This made me realize that mutexes, no matter how many I used could not do the job, so I ventured back into research of multithreading in C++ (Since PHP was basically not available) and on hours of trying to decipher references and tutorials, I ended learning a new concept - condition variables.

Condition Variables

Condition Variables, to the best of my understanding, basically works with a mutex to be able to control the flow of threads using boolean conditions.

Basically, this means that we can hold a thread temporarily until a condition is true. Not only that, other threads can be made to run while the current thread is on hold. This is done using the wait() and notify_all() functions.

Using this newly learnt concept, I ventured back into the challenge and came up with a different approach:

  1. Create a mutex and a condition variable for the methods fizz, buzz, fizzbuzz and number. Also create a counter i
  2. In each method, create a loop that runs through the numbers (1 to the given number n)
  3. Using the conditional variable, Using wait(), check the required conditions to the methods and also whether the counter exceeds the given number i.e. i > n
  4. If i > n, break the loop, else, print either 'fizz', 'buzz', 'fizzbuzz' or the number depending on the method used
  5. Increment the counter i
  6. Notify the remaining threads using notify_all().

It took a whole lot of time (for a while, I was not sure what I was doing), but it eventually came through, and after it being successful with the test cases, I submitted it and it worked!

6_accepted.png 6_accepted_2.png

ย