Snake Game
medium · simulation, collision rules
Brief-reported interview attribution (unverified): Microsoft, Amazon
How to approach an LLD roundRequirements
- Functional: move the snake, grow on food, end the game on a wall or self-collision (moving into the tail cell is allowed when not eating).
- Non-functional: the rules must be deterministic (food positions are passed in, never random); the game state must be inspectable (snake, score, over).
- Out of scope: rendering, input handling, speed/levels.
Statement
Implement the Snake game on a width × height grid, rows 0..height-1,
columns 0..width-1. The snake starts as a single cell at (0, 0), facing right.
Food positions are passed in as a fixed list (no randomness — the list is the
deterministic stand-in for the random food spawner). move(direction) returns
"ok", "ate" (the head landed on the current food position — the snake grows
by one and the next food appears), or "game over".
class SnakeGame:
def __init__(self, width, height, food):
# food: list of [row, col] positions, in the order they appear
def move(self, direction):
# direction: "up" | "down" | "left" | "right"
# -> "ok" | "ate" | "game over"
def snake(self):
# -> list of [row, col] from head to tail
def score(self):
# -> number of food items eaten
def is_over(self):
# -> True once the game has endedRules. Hitting a wall ends the game. Moving into the snake's own body ends the
game — except moving into the tail cell is allowed, because the tail moves away
in the same step (unless the snake is eating, in which case the tail stays and the
move is a collision). Once the game is over, every further move returns
"game over". When the food list is exhausted there is no more food to eat.
Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.
Class design (SOLID)
Snake (body as a deque plus an occupied set; SRP — head/tail updates and collision checks are O(1), and the tail cell is removed from the set before the new head is added so moving into the vacating tail cell still works). Arena (bounds and the food list; SRP). SnakeGame orchestrates the rules. No pattern is forced; the split (snake vs arena vs rules) is the point.
Public demo
# Public demo: run after implementing the class API above.
s = SnakeGame(5, 5, [(0, 1), (0, 2), (1, 2), (1, 1)])
print(s.move('right'))
print(s.snake())Reference solution and hidden tests
Locked. Open the interactive editor and attempt the problem; the reference, hidden test descriptions, pattern explanation and complexity are available only after an explicit reveal.
Concurrency
One game, one input at a time — no shared state beyond the game object. In a server version, one game per session with a lock per game around move.
Trade-offs & extensions
The body is a deque with an occupied set, so move is O(1). Extending: a FoodSpawner (deterministic list → random) and a GameRenderer interface so the same game can drive a terminal or a web UI.
Starter code
from collections import deque
class Snake:
def __init__(self):
"""food: list of [row, col] positions, in the order they appear"""
pass
def head(self):
"""Implement the return value and rules in the API contract."""
pass
def collides(self, cell, eating):
"""Implement the return value and rules in the API contract."""
pass
def move_to(self, new_head, eating):
"""Implement the return value and rules in the API contract."""
pass
class Arena:
def __init__(self, width, height, food):
"""food: list of [row, col] positions, in the order they appear"""
pass
def in_bounds(self, row, col):
"""Implement the return value and rules in the API contract."""
pass
def current_food(self):
"""Implement the return value and rules in the API contract."""
pass
def eat(self):
"""Implement the return value and rules in the API contract."""
pass
class SnakeGame:
def __init__(self, width, height, food):
"""food: list of [row, col] positions, in the order they appear"""
pass
def move(self, direction):
'''direction: "up" | "down" | "left" | "right" -> "ok" | "ate" | "game over"'''
pass
def snake(self):
"""-> list of [row, col] from head to tail"""
pass
def score(self):
"""-> number of food items eaten"""
pass
def is_over(self):
"""-> True once the game has ended"""
pass
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python interpreter