Code Room
CodingHardcod-g583
Subject Design data structuresLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Computer games, Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.