Design Snake Game - Problem

Design a Snake game that is played on a device with screen size height x width. The snake is initially positioned at the top left corner (0, 0) with a length of 1 unit.

You are given an array food where food[i] = (ri, ci) is the row and column position of a piece of food that the snake can eat. When a snake eats a piece of food, its length and the game's score both increase by 1.

Each piece of food appears one by one on the screen, meaning the second piece of food will not appear until the snake eats the first piece of food. When a piece of food appears on the screen, it is guaranteed that it will not appear on a block occupied by the snake.

The game is over if the snake goes out of bounds (hits a wall) or if its head occupies a space that its body occupies after moving (i.e. a snake of length 4 cannot run into itself).

Implement the SnakeGame class:

  • SnakeGame(int width, int height, int[][] food) Initializes the object with a screen of size height x width and the positions of the food.
  • int move(String direction) Returns the score of the game after applying one direction move by the snake. If the game is over, return -1.

Input & Output

Example 1 — Basic Snake Movement
$ Input: SnakeGame(3, 2, [[1,2],[0,1]]), move("R"), move("D"), move("R")
Output: [null, 0, 0, 1]
💡 Note: Initialize 3×2 board with food at (1,2) and (0,1). Move right to (0,1): no food, score=0. Move down to (1,1): no food, score=0. Move right to (1,2): eat food, score=1.
Example 2 — Hit Boundary
$ Input: SnakeGame(3, 2, []), move("L")
Output: [null, -1]
💡 Note: Snake starts at (0,0). Move left would go to (0,-1) which is out of bounds, so return -1.
Example 3 — Self Collision
$ Input: SnakeGame(2, 2, [[0,1],[1,1],[1,0]]), move("R"), move("D"), move("L"), move("U")
Output: [null, 1, 2, 3, -1]
💡 Note: Snake grows by eating food at (0,1), (1,1), (1,0). Final move up would hit its own body at (0,0), causing game over.

Constraints

  • 1 ≤ width, height ≤ 104
  • 1 ≤ food.length ≤ 50
  • food[i].length == 2
  • 0 ≤ ri < height
  • 0 ≤ ci < width
  • direction is 'U', 'D', 'L', or 'R'
  • At most 104 calls will be made to move

Visualization

Tap to expand
Snake Game Design OverviewGame Board (3×2)SFSnake (0,0)Food (1,2)Operations:move("R") → (0,1) score: 0move("D") → (1,1) score: 0move("R") → (1,2) EAT FOOD! score: 1Game Over Conditions:• Snake hits boundary walls• Snake head hits its own body• Return -1 when game endsResult: [null, 0, 0, 1] - Score increases when eating food
Understanding the Visualization
1
Initial Setup
Snake at (0,0) on width×height board with food positions
2
Movement
Snake moves in direction, checks bounds and self-collision
3
Game Logic
Handle food consumption, growth, and score updates
Key Takeaway
🎯 Key Insight: Combine queue for body order with hash set for instant collision detection
Asked in
Google 28 Amazon 22 Microsoft 15 Apple 12
28.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen