Day4-Solving a leetcode problem

Day4-Solving a leetcode problem

Question 1114 - Print in Order

Today,is a new day. My most difficult day solvinging a question given by #Ingressive4Good. It's probably due to the fact that the task required languages that were almost out of my league, but somehow i was able to solve it through multiple aggressive research.

Now let's see how I was able to do it

Question

Suppose we have a class:

public class Foo {
  public void first() { print("first"); }
  public void second() { print("second"); }
  public void third() { print("third"); }
}

The same instance of Foo will be passed to three different threads. Thread A will call first(), thread B will call second(), and thread C will call third(). Design a mechanism and modify the program to ensure that second() is executed after first(), and third() is executed after second().

Quite vague right?

The Approach

I had to reread the question multiple time to see that the objective was to fire three functions in the correct order without knowing how the system would schedule them, I realized that I needed to halt all of the function firing and establish a condition for when they should be fired.

So in my opinion, i had to use time factor to delay the execution of the method using the sleep.time( ) then saved the order of occurrence in a variable in order to know when each method finished executing and when they had finished being executed.

That way i was able to ensure that each method in the class was executed before the nexe in accordance with the given question.

Somehow i was able to keep the runtime and memory usage at minimum.

image.png

Conclusion

Since my understanding of Python was not very great, using it was a little challenging. However, it was enjoyable .

Well thank God the program ran successfully else day 3 would have been my end journey😂

Well that's all for now, if this blog post was any useful to you , Please give it a thumbs up.

You can also follow me on Twitter and Linkedin.

Until Next time, Bye for now.