0
$\begingroup$

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?

$\endgroup$
4
  • 1
    $\begingroup$ "how will you handle this?" is a very broad question for a very generic problem. This depends on the architecture of your application, how it is deployed etc. but also on the desired behavior. $\endgroup$
    – idmean
    Jul 24 at 10:44
  • $\begingroup$ So, what will you answer if these questions were asked to you in interview? $\endgroup$ Jul 24 at 10:54
  • 1
    $\begingroup$ That there is not enough information to make an informed decision. "How will you handle this" is an extremely broad question which puts no restrictions whatsoever on the answer. In fact, "doing nothing and letting the program crash" is also "handling it", and since there are no requirements placed on the outcome, is a perfectly valid answer, and by some measure even the best answer, since it costs nothing, requires no programmer resources, no code being written, no hardware, no testing, no update. $\endgroup$ Jul 24 at 17:17
  • $\begingroup$ @WarWithSelf If you found my answer useful, since more than a month has passed and no alternative has appeared, could you accept my answer? $\endgroup$
    – Chaos
    Aug 29 at 12:25
1
$\begingroup$

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,

$\endgroup$
1
  • $\begingroup$ I'm not disagreeing per se with what you say. But if I were to interview you, say for example, for a position working on a web application written in Java and you told me that you would actually implement a Bakery lock, this would make me a bit uneasy as well. Correctly implementing locks is a lot harder than it looks and Java already offers locks. Also, the DB might offer a better solution in some cases. As I said above – this question is very broad. $\endgroup$
    – idmean
    Jul 24 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.