| Users / Games | System Behavior | Data Size | Latency | Complexity |
|---|---|---|---|---|
| 100 games | Simple move validation, low concurrency | Minimal state per game | Instant response | Basic rule checks for special moves |
| 10,000 games | Increased concurrent validations, caching common states | Moderate memory for game states | Low latency with caching | Efficient rule evaluation needed |
| 1,000,000 games | High concurrency, distributed state management | Large memory and storage for game histories | Latency sensitive, needs optimization | Rule engine optimized, possible sharding by game |
| 100,000,000 games | Massive scale, global distribution | Huge storage, archival strategies | Latency critical, CDN for static assets | Microservices for move validation, event-driven updates |
Special moves (castling, en passant) in LLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
The first bottleneck is the move validation engine, especially for special moves like castling and en passant. These moves require checking complex conditions involving game state history and piece positions. As the number of concurrent games grows, the CPU and memory needed to validate moves in real-time become the limiting factor.
- Horizontal scaling: Run multiple validation servers behind a load balancer to handle concurrent move requests.
- Caching: Cache recent game states and validation results to avoid repeated complex calculations.
- Sharding: Partition games by user or game ID to distribute load and state management.
- Event-driven architecture: Use message queues to process move validations asynchronously when possible.
- Microservices: Separate special move validation logic into dedicated services for easier scaling and updates.
- At 1 million games with average 1 move per 10 seconds, expect ~100,000 QPS move validations.
- Each validation requires CPU cycles for rule checks; estimate 1 ms CPU per validation -> 100 CPU cores needed.
- Memory per game state ~1 KB; for 1 million games, ~1 GB RAM needed.
- Network bandwidth depends on move data size (~100 bytes); at 100,000 QPS -> ~10 MB/s bandwidth.
Start by explaining the special moves and their validation complexity. Then discuss how load grows with users and games. Identify the move validation engine as the bottleneck. Propose scaling solutions like caching, horizontal scaling, and sharding. Finally, mention trade-offs and monitoring strategies.
Your move validation service handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Add horizontal scaling by deploying more validation servers behind a load balancer to distribute the increased load and maintain low latency.
Practice
Solution
Step 1: Understand castling rules
Castling requires that neither the king nor the rook involved has moved before, and the squares between them are empty.Step 2: Check king safety conditions
The king cannot be in check, nor can it pass through or land on a square under attack during castling.Final Answer:
Neither the king nor the rook involved has moved before, and no pieces are between them. -> Option BQuick Check:
Castling conditions = Neither the king nor the rook involved has moved before, and no pieces are between them. [OK]
- Allowing castling when king is in check
- Ignoring if rook has moved
- Allowing king to move diagonally during castling
Solution
Step 1: Understand en passant position logic
En passant capture happens when an opponent's pawn moves two squares forward and lands beside your pawn horizontally.Step 2: Identify correct position offset
The opponent pawn must be exactly one square horizontally adjacent (x+1 or x-1), so position + (1, 0) is correct for right side.Final Answer:
if pawn.just_moved_two_squares and opponent_pawn.position == pawn.position + (1, 0): allow_en_passant() -> Option AQuick Check:
En passant horizontal check = if pawn.just_moved_two_squares and opponent_pawn.position == pawn.position + (1, 0): allow_en_passant() [OK]
- Checking vertical instead of horizontal adjacency
- Using wrong coordinate offsets
- Ignoring the two-square move condition
def can_castle(king_moved, rook_moved, path_clear):
if king_moved or rook_moved:
return False
if not path_clear:
return False
return True
print(can_castle(True, False, True))Solution
Step 1: Analyze input parameters
king_moved is True, rook_moved is False, path_clear is True.Step 2: Follow function logic
Since king_moved is True, the first if condition triggers and returns False immediately.Final Answer:
False -> Option CQuick Check:
King moved disables castling = False [OK]
- Ignoring king_moved condition
- Assuming path_clear overrides king_moved
- Expecting True despite king having moved
def can_en_passant(pawn_pos, opponent_pawn_pos, last_move):
if last_move == 'two_squares_forward' and abs(pawn_pos[0] - opponent_pawn_pos[0]) == 1:
return True
return False
# Example call
print(can_en_passant((4,4), (5,4), 'two_squares_forward'))Solution
Step 1: Understand en passant position requirements
En passant requires pawns to be on the same rank (same y-coordinate) and adjacent files (x-coordinates differ by 1).Step 2: Analyze code logic
The code checks x-coordinate difference but does not verify if y-coordinates are equal, missing a key condition.Final Answer:
The function does not check if pawns are on the same rank (y-coordinate). -> Option DQuick Check:
Missing same rank check = The function does not check if pawns are on the same rank (y-coordinate). [OK]
- Ignoring y-coordinate equality
- Assuming x difference alone suffices
- Misusing last_move parameter type
Solution
Step 1: Understand requirements for special moves
Castling and en passant depend on move history (e.g., whether king or rook moved, or if pawn moved two squares last turn) and current board state.Step 2: Evaluate design options
Tracking move history and board state allows accurate validation and supports scalability as game complexity grows.Final Answer:
Track each piece's move history and board state; validate special moves by checking move history and current board conditions. -> Option AQuick Check:
Move history + board state = correct scalable validation [OK]
- Ignoring move history for special moves
- Hardcoding rules without flexibility
- Skipping validation to simplify design
