Another Day, Another Challenge - More Leetcode Problems #Day4
Printing in Order
For the past few [days] (three days to be exact). I have been on a relatively flawless run. Unfortunately, today ruined my flawless streak. Let's get into it, shall we?
The Challenge
Is this really an easy challenge? ๐๐
Starting Approach
Honestly, I should have read between the lines as I greatly underestimated the challenge here in question. Using C++, I initially started with a simple conditional statement that utilizes boolean conditions. Something like this:
bool first=false, second=false;
function first(function printFirst){
first = true;
printFirst();
}
function second(function printSecond){
if(first != true)
this->first();
second = true;
printSecond();
}
function third(function printThird){
if(first != true)
this->first();
if(second != true)
this->second();
printThird();
}
Well, as simple as the code looked logically, it was a recipe for a whole lot of problems like the fact that the functions first()
, second()
and third()
use functions as parameters. This makes it difficult to reference their function parameters into the other methods calling it.
After time and time of frustrated attempts and pain (Almost cried a little ๐ฉ), I tried running the code and instead of returning the desired output, it kept giving random answers as output.
After failing to get anywhere, I took a step back and started looking at the question challenge question, The more I looked at the challenge the more I realized that there were some questions that rose up in my mind like: Why was it that PHP, my preferred language was not an option? Or why was it random? ๐ง
And then it hit me.
Second Approach (Working with time)
If one looks at the challenge, it uses the word asynchronous which means that whatever is going on here is working based on time. The word asynchronous means simultaneous or concurrent.
This means that all three methods of Foo
is being called upon at the same time hence even with the if statements, the results cannot be said certainly. So came the next question:
How do I stop a current method from working until another method is done?
Now, this led me to a very painful experience searching through methods of delaying functions based on time and then led me to my second approach to this challenge.
Basically, the idea behind this approach was to put time delays for the methods second()
and third()
. The time delay between these two methods will be based on which one is to be called first.
After research and lots of trial and error, I created a function based on the <ctime>
header file to create a pause()
function. Making my code look something like this:
function pause(timeInSeconds) {
// time delay function based on <ctime> header
}
function first(function printFirst){
printFirst();
}
function second(function printSecond){
delay(1);
printSecond();
}
function third(function printThird){
delay(2);
printThird();
}
Testing my code, it seemed to be working and everything seemed to be working with the test cases. All seemed to be going according to plan until I submitted and...
Why?!!!!! ๐ฅ
Final Approach
After it failed, I practically had to drop the laptop and take a break. I had put so much into it and all have come to no avail. I basically was at the brink of giving up on this challenge all together.
For the sake of saying that I least tried, I looked at the challenge question once again and I looked at the words carefully as I did previously. In doing so, I wondered at the wordsthreads making me to ask What are threads? This led to a whole lot of research and learning and threw me into a world I never knew.
Threads and Mutexes
Threads are basically pathways or routes for a program. We had already determined that the three methods of Foo
are running together all at once. This led to the question that solved it all: How do you pause or temporarily hold the flow of a thread? . The answer, Mutexes
Mutexes are features in C++ used to synchronize threads. This allows a method to controlled outside the influence of the flow or time of the program. We can basically used it to lock a thread in a program
To get familiar with the concept of this newly found solution, I practiced a bit on a seperate compiler and then went back to the challenge.
Using mutexes combined with intense thought, I came up with an algorithm that looked something like this:
declared mutex(es)
function first(function printFirst){
lockThread();
printFirst();
unlockThread();
}
function second(function printSecond){
lockThread();
printSecond();
unlockThread();
}
function third(function printThird){
lock(Thread);
printThird();
unlock(Thread);
}
Thankfully, at my lowest, I ran the code holding on to my prayers and thankfully it worked
I won, but at what cost?