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 available yet (food appears one by one), score=0. Move down to (1,1): still no food at this position, score=0. Move right to (1,2): eat first food piece, 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
Design Snake Game INPUT Grid: 3x2 (width x height) S Food Array: [[1,2], [0,1]] F1 Move Sequence: R D R Data Structures: Queue Snake body HashSet Body positions Snake Food ALGORITHM STEPS 1 Calculate New Head Apply direction to head pos 2 Check Boundaries Return -1 if out of bounds 3 Check Food If food: grow, else: remove tail 4 Check Self Collision Return -1 if head hits body Move Trace: R: (0,0)-->(0,1) Score: 0 D: (0,1)-->(1,1) Score: 0 R: Eats Food! Score: 1 Queue: [tail] <-- body <-- [head] (1,1) <-- (1,2) FINAL RESULT Final Game State: body head F2 (next) Output Array: [null, 0, 0, 1] Score Breakdown: init: null R: 0 D: 0 R: 1 (ate!) Game Active Snake Length: 2 Key Insight: Queue + HashSet for O(1) Operations The Queue maintains snake body order (FIFO): new head added to front, tail removed from back. HashSet enables O(1) collision detection by storing all body positions. When eating food, skip tail removal to grow. Check tail removal BEFORE collision check to handle edge case. TutorialsPoint - Design Snake Game | Queue + Hash Set Approach
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