Design Snake Game
🐍 What is Snake? Snake is the old phone game where you steer a line of squares around a grid. Every step the snake moves one cell forward. When its head lands on food, it eats it, grows one cell longer and scores a point. The game ends when the head hits a wall or the snake's own body.
Snake looks like a toy, which is exactly why interviewers like it. There are only two classes worth writing, so the whole conversation is about getting the rules exactly right and picking a data structure that makes every move cheap. One rule in particular, what happens when the head moves into the cell the tail is just leaving, separates candidates who traced their code from candidates who did not.
Requirements
"Design a Snake game. The snake moves on a grid, eats food to grow, and the game ends when it hits a wall or itself. Return the score after each move."
The prompt sounds complete, but almost every word hides a choice. These are the questions that change the code.
Clarifying Questions
You: How big is the board, and where does the snake start?
Interviewer: The board is width columns by height rows. Cells are (row, col) with (0, 0) in the top-left corner. The snake starts there with length 1.
You: Where does food come from? Random cells?
Interviewer: No randomness. You get a list of food positions up front. Only one piece is on the board at a time: the first one in the list, and when it is eaten the next one appears. A new piece never appears on a cell the snake is on.
You: Is the game driven by a clock, or by calls?
Interviewer: By calls. Each move(direction) is one step. Return the score after the step, or -1 if the step ended the game.
You: What happens if the head moves into the cell where the tail is right now?
Interviewer: That is fine. On the same step the tail moves forward, so the cell is empty by the time the head gets there. Unless the snake just ate, of course: then the tail stays put.
You: And when the snake eats the last piece in the list?
Interviewer: The game goes on. There is just no more food, so the score stops changing.
You: What should move return once the game is over?
Interviewer: Keep returning -1. The game does not restart.
The fourth answer is the one to remember. It decides the order of the steps inside move, and it is the bug most first drafts have. We will trace it carefully in the Implementation section.
💡 Interview tip: For any grid game, ask about the coordinate system first: which way rows go, whether positions are
(row, col)or(x, y), and where(0, 0)is. Mixing upwidthandheightis the most common way to fail every test with code that "looks right".
Final Requirements
- The board has
heightrows andwidthcolumns. The snake starts at(0, 0)with length 1 and score 0. move(direction)moves the head one cell:"U"up,"D"down,"L"left,"R"right.- If the head leaves the board, the game is over.
- Food appears one piece at a time, in the order given. If the head lands on the current piece, the snake eats it: the score goes up by one, the snake grows by one cell, and the next piece appears.
- If the snake did not eat, its tail moves forward on the same step, so the head may enter the cell the tail just left.
- If the head lands on any other part of the body, the game is over.
movereturns the score, or-1if the game is over (on that move or earlier).
Out of scope
- Drawing the board, keyboard input and a real-time clock.
- Random food, obstacles and levels (we come back to these in Extensibility).
- Several snakes or players.