Execute Turn-Based Movement Commands on a Line
Data Structure DesignAuto-graded · 18 test casesLast updated
Tests state tracking and rule validation for alternating player moves on a bounded one-dimensional board.
You are building a controller for a two-player board game on a one-dimensional track with positions numbered 0 through n-1. Player 1 begins at position 0 and Player 2 begins at position n-1.
Players alternate turns, with Player 1 moving first. On each turn, the active player attempts to shift their piece by -1 (left), 0 (stay), or +1 (right).
A move succeeds if and only if all of the following hold:
- It is the correct player's turn.
- The destination position lies within
[0, n-1]. - The destination position is not currently occupied by the other player.
When a move succeeds, the player's position updates and control passes to the opponent. When a move fails, the board state remains unchanged and the same player must try again.
Your task is to process a sequence of initialization and move commands, returning the outcome of each operation.
Input Format
You will receive two parallel lists:
operations: a list of command names, each either"GameSimulator"or"move".arguments: a list of argument lists, one per command.
For "GameSimulator" commands, the argument list contains a single integer [n] specifying the board size. This command initializes (or reinitializes) the game.
For "move" commands, the argument list contains [player_id, direction], where player_id is 1 or 2 and direction is -1, 0, or +1.
Output Format
Return a list of results, one per command:
- For
"GameSimulator", appendNone(rendered asnullin JSON). - For
"move", append a two-element list[is_valid, game_over]where both are booleans.
is_valid is True if the move was accepted, False otherwise. game_over indicates whether the game has ended (note: under the occupancy rule, this will always be False because players cannot land on each other).
Constraints
2 ≤ n ≤ 10001 ≤ len(operations) ≤ 1000player_idis always1or2directionis always-1,0, or+1- The first operation is always
"GameSimulator"
Examples
Example 1
Input: execute_board_commands(['GameSimulator', 'move', 'move'], [[5], [1, 1], [2, -1]])
Output: [None, [True, False], [True, False]]
Example 2
Input: execute_board_commands(['GameSimulator', 'move', 'move', 'move'], [[4], [1, 1], [1, 1], [2, -1]])
Output: [None, [True, False], [False, False], [True, False]]
Hints
Hint 1
Track three pieces of state: Player 1's position, Player 2's position, and whose turn it is next.
Hint 2
An invalid move must not change any state—positions and turn order stay exactly as they were.
Hint 3
Check all three validity conditions before updating anything. The opponent's position is at index 3 minus the current player's ID.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires maintaining game state across a sequence of commands and validating each move against three independent rules.
The state consists of exactly three values: the position of Player 1, the position of Player 2, and which player has the next turn. When a "GameSimulator" command arrives, we reset these values to their starting configuration—Player 1 at 0, Player 2 at n-1, and Player 1 to move.
For each "move" command, we compute the destination square and check three conditions:
- Turn order: The
player_idin the command must match the player whose turn it currently is. - Bounds: The destination must lie within
[0, n-1]. - Occupancy: The destination must not be the opponent's current position.
All three checks run before any state change. If all pass, we update the moving player's position and flip the turn to the opponent (using 3 - player_id to toggle between 1 and 2). If any check fails, we leave the state untouched and return [False, False]. Importantly, the turn does not advance after a failed move, so the same player must retry.
The second boolean in the result, game_over, is always False. The occupancy rule prevents a player from ever landing on the opponent's square, which means the capture condition defined in the original game rules can never be satisfied.
Why It Works
- Turn enforcement: We only flip
current_turnon valid moves, ensuring strict alternation starting with Player 1. - Atomicity of invalid moves: All validation happens before any mutation, so rejected moves have no side effects.
- Distinctness: The two players start at different positions (
0andn-1wheren ≥ 2), and the occupancy check guarantees they never occupy the same square. This also means moving zero steps (staying put) is always legal on your turn. - No deadlock: A player can always choose
direction = 0to pass their turn without violating any rule.
Complexity
- Time: O(1) per command, O(k) total for k commands.
- Space: O(1) for game state, O(k) for the output list.
Solution
from typing import List, Optional
def execute_board_commands(operations: List[str], arguments: List[List[int]]) -> List[Optional[List[bool]]]:
results = []
board_size = None
player_positions = [None, None, None] # indices 1 and 2 for players 1 and 2
current_turn = 1
for command, args in zip(operations, arguments):
if command == "GameSimulator":
board_size = args[0]
player_positions[1] = 0
player_positions[2] = board_size - 1
current_turn = 1
results.append(None)
elif command == "move":
player_id, shift = args
target_position = player_positions[player_id] + shift
opponent_id = 3 - player_id
is_valid = (
player_id == current_turn
and 0 <= target_position < board_size
and target_position != player_positions[opponent_id]
)
if is_valid:
player_positions[player_id] = target_position
current_turn = opponent_id
# Since moving onto an occupied square is forbidden, players cannot
# capture each other under these rules, so game_over remains False
results.append([is_valid, False])
return results