Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #353: Design Snake Game

In this guide, we solve Leetcode #353 Design Snake Game in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

Design a Snake game that is played on a device with screen size height x width. Play the game online if you are not familiar with the game.

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: Design, Queue, Array, Hash Table, Simulation

Intuition

Fast membership checks and value lookups are the heart of this problem, which makes a hash map the natural choice.

By storing what we have already seen (or counts/indexes), we can answer the question in one pass without backtracking.

Approach

Scan the input once, using the map to detect when the condition is satisfied and to update state as you go.

This keeps the solution linear while remaining easy to explain in an interview setting.

Steps:

  • Initialize a hash map for seen items or counts.
  • Iterate through the input, querying/updating the map.
  • Return the first valid result or the final computed value.

Example

Input ["SnakeGame", "move", "move", "move", "move", "move", "move"] [[3, 2, [[1, 2], [0, 1]]], ["R"], ["D"], ["R"], ["U"], ["L"], ["U"]] Output [null, 0, 0, 1, 1, 2, -1] Explanation SnakeGame snakeGame = new SnakeGame(3, 2, [[1, 2], [0, 1]]); snakeGame.move("R"); // return 0 snakeGame.move("D"); // return 0 snakeGame.move("R"); // return 1, snake eats the first piece of food. The second piece of food appears at (0, 1). snakeGame.move("U"); // return 1 snakeGame.move("L"); // return 2, snake eats the second food. No more food appears. snakeGame.move("U"); // return -1, game over because snake collides with border

Python Solution

class SnakeGame: def __init__(self, width: int, height: int, food: List[List[int]]): self.m = height self.n = width self.food = food self.score = 0 self.idx = 0 self.q = deque([(0, 0)]) self.vis = {(0, 0)} def move(self, direction: str) -> int: i, j = self.q[0] x, y = i, j match direction: case "U": x -= 1 case "D": x += 1 case "L": y -= 1 case "R": y += 1 if x < 0 or x >= self.m or y < 0 or y >= self.n: return -1 if ( self.idx < len(self.food) and x == self.food[self.idx][0] and y == self.food[self.idx][1] ): self.score += 1 self.idx += 1 else: self.vis.remove(self.q.pop()) if (x, y) in self.vis: return -1 self.q.appendleft((x, y)) self.vis.add((x, y)) return self.score # Your SnakeGame object will be instantiated and called as such: # obj = SnakeGame(width, height, food) # param_1 = obj.move(direction)

Complexity

The time complexity is O(n). The space complexity is O(n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy