Implement a Player Selection Validation System

Difficulty: EASYCategory: LLDFrequency: 1Asked On 2025-04-01

Problem Statement

Design and implement a validation system for selecting players in a fantasy sports platform (like Dream11). The system must enforce all rules and constraints related to team composition, credits, roles, and real-life team distribution before allowing submission.


Functional Requirements


1.<span></span>Team Composition Constraints
2.<span></span>Exactly 11 players must be selected.
3.<span></span>Each player belongs to one of the roles:
4.<span></span>Batsman (BAT)
5.<span></span>Bowler (BWL)
6.<span></span>All-rounder (AR)
7.<span></span>Wicket-keeper (WK)
8.<span></span>Role-based Minimum/Maximum Rules (example: for cricket)
9.<span></span>WK: 1–4
10.<span></span>BAT: 3–6
11.<span></span>AR: 1–4
12.<span></span>BWL: 3–6
13.<span></span>Credits Constraint
14.<span></span>Each player has a credit value (e.g., 8.5).
15.<span></span>Total credits of all selected players must not exceed 100 credits.
16.<span></span>Team Distribution Constraint
17.<span></span>Players come from two real teams (Team A vs Team B).
18.<span></span>A fantasy team can have a maximum of 7 players from any single real team.
19.<span></span>Captain & Vice-Captain Selection
20.<span></span>Must designate exactly 1 captain and 1 vice-captain.
21.<span></span>They must be distinct players.
22.<span></span>Validation API
23.<span></span>validateTeam(selectedPlayers) → ValidationResult
24.<span></span>Should return:
25.<span></span>success=true/false
26.<span></span>Error messages if constraints are violated (e.g., "Team exceeds credit limit", "Need at least 3 batsmen").


Example Input


Selected players:

<div><div>yaml</div><div>CopyEdit</div><div>[</div><div> {id: 1, role: "BAT", credit: 9.0, team: "A"},</div><div> {id: 2, role: "BAT", credit: 8.5, team: "A"},</div><div> {id: 3, role: "BWL", credit: 8.0, team: "B"},</div><div> ...</div><div>]</div></div>

Captain = Player 1

Vice-Captain = Player 3

Example Output

✅ If valid:

<div><div>yaml</div><div>CopyEdit</div><div>{ success: true, message: "Team selection is valid" }</div></div>

❌ If invalid:

<div><div>yaml</div><div>CopyEdit</div><div>{ success: false, message: "Total credits exceeded 100" }</div></div>


Non-Functional Requirements


1.<span></span>Should handle real-time validation as players are added/removed.
2.<span></span>Must be thread-safe if multiple users are validating simultaneously.
3.<span></span>Should be extensible to support different sports (football, kabaddi) with different role constraints.


Deliverables


1.<span></span>Class/Function design (e.g., Player, TeamValidator, ValidationRule).
2.<span></span>Implementation of validation logic with clear error messages.
3.<span></span>Unit tests covering:
4.<span></span>Valid team
5.<span></span>Credit overflow
6.<span></span>Role constraint violation
7.<span></span>More than 7 players from one real team
8.<span></span>Missing captain/vice-captain


Solution

Comments