0

I'm an experienced Software Engineer but very weak in concurrency because of no prior experience in that. I've been interviewing with several companies in which I was asked similar kinds of questions as given below:

  1. If you are designing a fantasy sports application in which there's a contest that can handle only 100 users. If 99 users are already registered for the contest and multiple users hit the PARTICIPATE button at the same time to become the 100th user, then how will you handle this in your application?

  2. If you are designing a chess game, where multiple users are selecting the users of the same level of competence to play with them. Then suppose at the same time, users A and B choose user C (to play with) at the same time, then how will you handle this?

I usually answer this by saying that I'll use synchronized block in Java or on the Database side, I'll use the Locking concept. But I'm not sure of either of them. So, can anyone tell here how do you answer such questions? Should one answer this in terms of Java Multithreading or DBMS or both? Please can anyone also tell me any resources to understand the theory to answer such types of questions or any website or YouTube channel where solutions are given to these kinds of questions?

1 Answers1

1

my answer to the first problem would go like this:

From a logical perspective, it depends on the actual purpose of the application, however, we should at least ensure safety and liveness, therefore some locking algorithms are appropriate. Since the contest could be very serious (legal consequences in case of booking failure) I would opt for a mutual exclusive (ensures safety, namely at most one user will book as the 100th competitor), starvation free (ensures everybody will eventually access the lock), fifo (ensures that users can not overcome each other at booking time and they are served in the order they show up) lock.

From a programming perspective, I would first ask about precise requirements because mentioned use-cases seem to be easily manageable through simple and famous (but not scalable) locking algorithms. The Bakery Lock (Wikipage) satisfies all the properties I mentioned earlier but comes with several disadvantages, namely, it must read as many variables as participant processes. Its implementation in Java should be easy and there are several examples on the Internet.

The philosophy of my answer is the one employed in "The garbage collection handbook", namely: safety properties must be honored but your design decisions should depend on use-cases; provided hard requirements are to be met more sophisticated algorithms are to be employed.

If you are interested in learning more about concurrency there exist several books about it, I name just two: The Art of Multiprocessor Programming (chapters from 1 to 10 should suffice), by Shavit & Herlihy Distributed Algorithms (chapter 10 is about locking), by Nancy Lynch

Regards,

Chaos
  • 577
  • 3
  • 12