Design Snake Game - Problem
๐ Design Snake Game
Design and implement the classic Snake Game that runs on a device with screen dimensions height ร width. If you're not familiar with Snake, it's a simple but addictive game where a snake moves around the screen eating food and growing longer!
Game Rules:
- ๐ The snake starts at position
(0, 0)(top-left corner) with length 1 - ๐ Food appears one piece at a time at predetermined locations
- ๐ When the snake eats food, both its length and score increase by 1
- ๐ Game ends if the snake hits a wall or collides with itself
- ๐ฏ Return the current score after each move, or
-1if game over
Your task: Implement the SnakeGame class with:
SnakeGame(int width, int height, int[][] food)- Initialize the gameint move(String direction)- Move snake and return score (or -1 if game over)
Directions: "U" (up), "D" (down), "L" (left), "R" (right)
Input & Output
example_1.py โ Basic Snake Movement
$
Input:
SnakeGame(3, 2, [[1,2],[0,1]])
snake.move("R") # Move right
snake.move("D") # Move down
snake.move("R") # Move right (eat food)
snake.move("U") # Move up (eat food)
snake.move("L") # Move left
snake.move("U") # Move up (hit wall)
โบ
Output:
0
0
1
2
2
-1
๐ก Note:
Snake moves through the 3x2 grid. Score increases when food is eaten at (1,2) and (0,1). Game ends when snake tries to move up from row 0 (out of bounds).
example_2.py โ Self Collision
$
Input:
SnakeGame(3, 3, [[2,0]])
snake.move("D") # Move down
snake.move("D") # Move down (eat food)
snake.move("U") # Move up
snake.move("U") # Move up (hit itself)
โบ
Output:
0
1
1
-1
๐ก Note:
Snake grows after eating food at (2,0). When it tries to move back up, it collides with its own body segment, ending the game.
example_3.py โ Empty Food Array
$
Input:
SnakeGame(2, 2, [])
snake.move("R") # Move right
snake.move("D") # Move down
snake.move("L") # Move left
snake.move("U") # Move up (back to start)
โบ
Output:
0
0
0
0
๐ก Note:
No food available, so snake maintains length 1 and score stays 0. Snake moves in a square pattern without growing.
Visualization
Tap to expand
Understanding the Visualization
1
Initialize Game
Start with snake head at (0,0), empty food queue, score = 0
2
Process Move
Calculate new head position based on direction
3
Validate Move
Check bounds and collision using hash set O(1) lookup
4
Update Snake
Add new head, conditionally remove tail, sync data structures
5
Return Score
Return current score or -1 if game over
Key Takeaway
๐ฏ Key Insight: Combining deque for efficient queue operations with hash set for instant collision detection creates an optimal O(1) solution that scales perfectly for competitive gaming scenarios.
Time & Space Complexity
Time Complexity
O(1)
Each move operation is O(1) - hash set lookup, deque operations are all constant time
โ Linear Growth
Space Complexity
O(n)
Store snake coordinates in both deque and hash set, where n is snake length
โก Linearithmic Space
Constraints
- 1 โค width, height โค 104
- 0 โค food.length โค 50
- food[i].length == 2
- 0 โค ri < height
- 0 โค ci < width
- direction.length == 1
- direction is one of {'U', 'D', 'L', 'R'}
- At most 104 calls will be made to move
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code