Design the Backend Model and Validation Rules for a Fantasy Team
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-02-01
Problem Statement
Design the backend data model and validation rules for a fantasy sports platform where users create teams of players under specific constraints. The system should ensure teams are valid before submission and be flexible enough to support multiple sports (e.g., cricket, football, kabaddi).
Functional Requirements
1. Team Composition
1.<span></span>A fantasy team must have a fixed number of players (e.g., 11 for cricket, 7 for kabaddi).
2.<span></span>Each player belongs to a role (e.g., batsman, bowler, wicketkeeper, goalkeeper, defender, etc.).
3.<span></span>Role-based minimum and maximum limits should be enforced (e.g., cricket: 1–4 wicketkeepers, 3–6 batsmen).
2. Credit System
1.<span></span>Each player has a credit value (e.g., 8.5).
2.<span></span>Total credits of the selected team must not exceed a defined credit cap (e.g., 100 credits).
3. Real Team Distribution
1.<span></span>Players belong to real-world teams (e.g., Team A vs Team B).
2.<span></span>A fantasy team can select at most X players (e.g., 7) from a single real team.
4. Captain & Vice-Captain Rules
1.<span></span>Each fantasy team must assign one captain and one vice-captain.
2.<span></span>Captain and vice-captain must be distinct players.
5. Validation Rules
1.<span></span>Must validate:
2.<span></span>Correct number of players.
3.<span></span>Role constraints (min/max per role).
4.<span></span>Credit limit not exceeded.
5.<span></span>Team distribution rule not violated.
6.<span></span>Captain/vice-captain assignment.
7.<span></span>Should provide clear error messages if validation fails.
6. API Endpoints (Example)
1.<span></span>POST /team/validate → Validate a user’s fantasy team.
2.<span></span>POST /team/submit → Save the team if validation passes.
3.<span></span>GET /team/{userId} → Fetch user’s submitted team.
Data Model (Example for Cricket)
Player
1.<span></span>id (unique identifier)
2.<span></span>name
3.<span></span>role (BAT, BWL, AR, WK)
4.<span></span>credit (float)
5.<span></span>realTeam (string)
FantasyTeam
1.<span></span>id
2.<span></span>userId
3.<span></span>players: List<Player>
4.<span></span>captainId
5.<span></span>viceCaptainId
6.<span></span>totalCredits
Non-Functional Requirements
1.<span></span>Validation must be fast to support real-time feedback on the team selection screen.
2.<span></span>Backend must be scalable to handle millions of users submitting teams simultaneously before match deadlines.
3.<span></span>Model should be extensible for multiple sports with different rules.
Deliverables
1.<span></span>Backend data model (class/entity design).
2.<span></span>Validation rules engine with pluggable constraints.
3.<span></span>API design for validation and submission.
4.<span></span>Unit tests covering different validation scenarios.