Code RoomSnake game
HardPrep Room Coding #1150

Snake game

CodingAlgorithms & data structuresSenior–Staff~35 min

Simulate the Snake game on a grid of given width and height. The snake starts length 1 at (row 0, col 0). Food positions are given in order as [row, col] pairs; the snake eats the next food only when its head lands exactly on it. Given a sequence of moves (each 'U','D','L','R'), return the score (food eaten) after the LAST successfully applied move, or -1 as soon as a move kills the snake (running into a wall or its own body). Eating food grows the snake by 1 (tail does not move that step). The snake's body is its own obstacle except that the tail cell vacates as the head advances (unless it just ate).

Implement
snake_game(width: int, height: int, food: list[list[int]], moves: str) → int
Examples
in[3,2,[[1,2],[0,1]],"RDRUL"]out2
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 35 min
InputExpectedGot
[3,2,[[1,2],[0,1]],"RDRUL"]2not run yetsample