As reported: Atlassian SDE-2 (2025), round 1: a snake game on a square board of any size, behind
interface SnakeGame { void moveSnake(Direction d); boolean isGameOver(); }. The snake starts with size 3, grows by 1 every 5 moves, and the game is over when the snake hits itself. Moves are up, down, left, right; assume valid moves; the snake moves only whenmoveSnakeis called.
Implement SnakeGame:
SnakeGame(boardSize)— ann × nboard (n = boardSize). The snake occupies(0,0),(0,1),(0,2)(row, column) with its head at(0,2)and tail at(0,0).moveSnake(direction)—"U","D","L"or"R"moves the head one cell. The board wraps around: leaving one edge enters at the opposite edge.isGameOver()—trueonce the snake has hit itself.
On every 5th move (moves 5, 10, 15, …) the snake grows: the head advances and the tail stays. On other moves the tail cell is freed as the head advances, so the head may move into the cell the tail just left. If the head moves onto any other cell of the body, the game is over; later calls to moveSnake do nothing.
Example 1
SnakeGame(4); moveSnake "R" five times, checking isGameOver after each move
Output: false, false, false, false, false
The head wraps from column 3 to column 0, and the snake has length 4 after move 5.
Example 2
SnakeGame(5); moves D L U R D L U R D L
isGameOver after each move: false ×9, then true
After move 5 the snake (length 4) fills a 2×2 square and keeps circling it. On move 10 it grows, so its tail does not move out of the way, and the head bites the body.
Constraints: 3 ≤ boardSize ≤ 1000; at most 2·10^4 moves. Moves are always valid: a move is never the opposite of the previous move (and the first move is never "L").
Code runner format: two lines in LeetCode's design format: the method names ("SnakeGame", "moveSnake", "isGameOver") and their arguments ([boardSize] for the constructor). The harness prints the results as a JSON array (null for the constructor and moveSnake).