educative.io

Https://discuss.educative.io/tag/uber-ride-problem__interview-practice-problems__java-multithreading-for-senior-engineering-interviews

Why have we initialized the Semaphore with 0 ? why couldn’t we initialize it with 3 ?
How are threads going to acquire it if there are no permits ?

Hi @Mayank_Jain!

We initialize the semaphore with 0 because we want to initially block their access to the rides and therefore, will require a release first. The semaphores are acquiring this permit from the democrats++; and republicans++; code lines in the respective functions of both seatDemocrat and seatRepublican. One of this example can also be seen in the image given below:

If we try initializing with 3, then that means there are already 3 waiting democrats in the queue and hence, the other conditions won’t be fulfilled.

I hope it helps!