← Create New Paste

Designing the Snake Game in an Object-Oriented Design Interview

3 views
"Design the classic Snake game" shows up in low-level design rounds because it is small enough to finish in 45 minutes but still tests whether you can separate state, rules and rendering. Here is a walkthrough you can adapt. 1. Clarify the rules first Spend two or three minutes asking questions before drawing any classes: - Is the board a fixed grid? What happens at the edges: wall collision or wrap-around? - Does food appear one item at a time, at a random empty cell? - Does the snake grow by one cell per food item? - Is it single player? Are there levels or speed increases? - Do we need rendering, or only the game engine with a step() function? Write the agreed rules in one corner of the whiteboard or doc. Interviewers like seeing the scope pinned down. 2. Identify the core objects A clean first cut: - Position: an immutable (row, col) pair with equality and hashing. - Direction: an enum UP, DOWN, LEFT, RIGHT with a delta and an isOpposite() check. - Snake: owns a deque of Positions (head at the front) plus a hash set of occupied cells for O(1) collision checks. - Board: width, height and a method to check if a position is inside the grid. - FoodSpawner: picks a random empty cell; inject the random source so tests are deterministic. - Game: holds the Board, Snake, current food, score and state (RUNNING, GAME_OVER), and exposes changeDirection() and step(). 3. The step() function is the heart of the answer On each tick: 1. Compute the next head from the current head plus the direction delta. 2. If it is outside the board (or wrap it, depending on the rules), the game is over. 3. Check if the next head is food. 4. If not food, remove the tail from the deque and the set first. This detail matters: the snake can legally move into the cell its tail is leaving. 5. If the next head is already in the occupied set, the game is over. 6. Add the new head to the deque and the set. 7. If it was food, increase the score and spawn new food. Saying out loud why you remove the tail before the self-collision check is the kind of edge case that separates a strong answer from an average one. 4. Handle input safely Ignore a direction change that is the exact opposite of the current one, otherwise a fast keypress makes the snake reverse into itself. If several key presses can arrive between ticks, buffer at most one or two in a small queue so input feels responsive without skipping moves. 5. Complexity and extensions Each step is O(1) thanks to the deque and the set. Food spawning is the only part that can get slow on a nearly full board; mention keeping a list of empty cells or sampling with a fallback scan. Common follow-ups: - Obstacles: add a set of blocked cells to the Board. - Levels and speed: a Level config object, so the Game does not need if-statements for each level. - Multiplayer: several Snake objects and a collision rule between them. - Rendering: keep a Renderer interface separate from Game, so the engine can be tested without a UI. This is a natural place to mention the observer pattern. 6. How to present it Start with the class list, then the step() logic, then a short test you would write: "snake of length 3 moving right eats food and becomes length 4". Keep talking while you design so the interviewer can follow your reasoning and redirect you early. If you freeze when the interviewer adds a follow-up you did not expect, real-time support can help. Linkjob AI (https://linkjob.ai) gives real-time help during live job interviews and technical interviews, including design rounds like this one. But the best preparation is still designing Snake once end to end on your own before the real interview.
Created Nov 17, 2026 04:35 Expires Nov 17, 2027 04:35