India Pakistan rivalry

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-04-09

Problem Statement

India and Pakistan are participating in a bilateral cricket series. Each team has 11 players, and due to mutual hostility, players from the two teams do not wish to share the practice field at the same time.

To prevent conflicts, both captains agree on a rule:

1.A player can enter the practice field only if it is currently empty or already occupied by players from their own team.
2.If any player sees a member of the opposing team on the field, they must wait until the field is completely empty before entering.

Your task is to implement a thread-safe concurrency solution that enforces this rule, ensuring players from both teams can practice safely without overlapping.

Follow-up:

How would you modify your solution to prevent starvation? For example, it is possible that one team (e.g., India) keeps entering the field back-to-back, never giving the other team (e.g., Pakistan) a chance to practice. Design your solution to ensure fairness and avoid such indefinite delays.


NOTE: do not use any fancy library except locks and signals or conditional variables.

Solution

1// sample java solution 2 3import java.util.concurrent.locks.Condition; 4import java.util.concurrent.locks.ReentrantLock; 5 6public class PracticeField { 7 private final ReentrantLock lock = new ReentrantLock(true); // fair lock 8 private final Condition indiaCondition = lock.newCondition(); 9 private final Condition pakistanCondition = lock.newCondition(); 10 11 private int indiaCount = 0; 12 private int pakistanCount = 0; 13 14 private enum Team { INDIA, PAKISTAN } 15 private Team currentTeam = null; 16 17 public void enterField(Team team) throws InterruptedException { 18 lock.lock(); 19 try { 20 while (currentTeam != null && currentTeam != team) { 21 if (team == Team.INDIA) { 22 indiaCondition.await(); 23 } else { 24 pakistanCondition.await(); 25 } 26 } 27 28 if (team == Team.INDIA) { 29 indiaCount++; 30 currentTeam = Team.INDIA; 31 } else { 32 pakistanCount++; 33 currentTeam = Team.PAKISTAN; 34 } 35 } finally { 36 lock.unlock(); 37 } 38 } 39 40 public void leaveField(Team team) { 41 lock.lock(); 42 try { 43 if (team == Team.INDIA) { 44 indiaCount--; 45 if (indiaCount == 0) { 46 currentTeam = null; 47 pakistanCondition.signalAll(); // allow Pakistan to proceed 48 } 49 } else { 50 pakistanCount--; 51 if (pakistanCount == 0) { 52 currentTeam = null; 53 indiaCondition.signalAll(); // allow India to proceed 54 } 55 } 56 } finally { 57 lock.unlock(); 58 } 59 } 60} 61 62public class Player extends Thread { 63 private final PracticeField field; 64 private final PracticeField.Team team; 65 66 public Player(PracticeField field, PracticeField.Team team) { 67 this.field = field; 68 this.team = team; 69 } 70 71 @Override 72 public void run() { 73 try { 74 field.enterField(team); 75 System.out.println(team + " player " + Thread.currentThread().getId() + " entered field."); 76 Thread.sleep(1000); // simulate practice time 77 System.out.println(team + " player " + Thread.currentThread().getId() + " leaving field."); 78 field.leaveField(team); 79 } catch (InterruptedException e) { 80 Thread.currentThread().interrupt(); 81 } 82 } 83} 84 85public class Main { 86 public static void main(String[] args) { 87 PracticeField field = new PracticeField(); 88 89 // Create and start 11 India and 11 Pakistan players 90 for (int i = 0; i < 11; i++) { 91 new Player(field, PracticeField.Team.INDIA).start(); 92 new Player(field, PracticeField.Team.PAKISTAN).start(); 93 } 94 } 95} 96 97


Comments