Snake Game – Atlassian Variation

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-05-01

Problem Statement

Design a modified version of the classic Snake game with updated mechanics tailored to Atlassian's rules.

You are given a 2D grid of dimensions height x width. The snake starts at position (0, 0) with an initial length of 1. Each move is in one of the four directions: up ("U"), down ("D"), left ("L"), or right ("R").


Game Rules:

1.Movement and Rollover: If the snake moves beyond the grid’s boundary in any direction, it rolls over to the opposite side. Example: Moving right from the last column places the head at the first column of the same row.
2.Scoring and Growth: The snake grows by 1 unit automatically for every 5 points scored (i.e., at scores 5, 10, 15, etc.).
3.Game Over Condition: The game ends if the snake runs into itself.


Class Definition:

Implement the class SnakeGame:

1SnakeGame(int width, int height)

Initializes the game board with the given dimensions.

1int move(String direction)

Moves the snake in the specified direction.

Returns the current score after the move.

Returns -1 if the game is over due to self-collision


Example Usage:

SnakeGame game = new SnakeGame(3, 3);

game.move("R"); // return 1

game.move("D"); // return 2

game.move("D"); // return 3

game.move("L"); // return 4

game.move("U"); // return 5

game.move("L"); // return 6 (snake grows to length 2)

Solution

Approach:

To design the Snake Game with Atlassian-specific rules, we use a deque to track the snake’s body positions, where the front represents the head and the back represents the tail. This allows efficient additions and removals as the snake moves or grows. We also use a hash set to store occupied positions for constant-time collision checks. On each move, we compute the new head position based on the direction and apply modular arithmetic to implement wraparound when the snake hits a grid boundary. Before checking for self-collision, we temporarily remove the tail from the occupied set (if the snake is not growing), since it’s about to move. If the new head position is already in the occupied set, the game ends due to self-collision. Otherwise, we add the new head to the deque and the occupied set. If the snake is growing (every 5th point), we skip removing the tail to increase the length. This approach ensures efficient movement, wraparound behavior, collision detection, and growth management, all in optimal time complexity.


Code:

1#include <iostream> 2#include <deque> 3#include <unordered_set> 4#include <string> 5using namespace std; 6 7class SnakeGame { 8private: 9 int width, height, score; 10 int growThreshold; 11 deque<pair<int, int>> snake; // stores the snake's body (head at front) 12 unordered_set<string> occupied; // stores occupied positions 13 int growthCountdown; 14 15 pair<int, int> moveInDirection(pair<int, int> head, char dir) { 16 int x = head.first; 17 int y = head.second; 18 if (dir == 'U') x--; 19 else if (dir == 'D') x++; 20 else if (dir == 'L') y--; 21 else if (dir == 'R') y++; 22 // Wrap around 23 x = (x + height) % height; 24 y = (y + width) % width; 25 return {x, y}; 26 } 27 28 string posToStr(int x, int y) { 29 return to_string(x) + "," + to_string(y); 30 } 31 32public: 33 SnakeGame(int width, int height) { 34 this->width = width; 35 this->height = height; 36 score = 0; 37 growThreshold = 5; 38 growthCountdown = 5; 39 40 snake.push_back({0, 0}); 41 occupied.insert(posToStr(0, 0)); 42 } 43 44 int move(string direction) { 45 char dir = direction[0]; 46 pair<int, int> currentHead = snake.front(); 47 pair<int, int> newHead = moveInDirection(currentHead, dir); 48 string newHeadStr = posToStr(newHead.first, newHead.second); 49 50 // Remove tail from occupied temporarily (since it will move unless we're growing) 51 pair<int, int> tail = snake.back(); 52 string tailStr = posToStr(tail.first, tail.second); 53 if (growthCountdown != 0) { 54 occupied.erase(tailStr); 55 } 56 57 // Check for collision 58 if (occupied.count(newHeadStr)) return -1; 59 60 // Add new head 61 snake.push_front(newHead); 62 occupied.insert(newHeadStr); 63 64 // If we are not growing, remove tail 65 if (growthCountdown != 0) { 66 snake.pop_back(); 67 } else { 68 // Just finished growing, reset growth countdown 69 growthCountdown = growThreshold; 70 } 71 72 // Update score and growth countdown 73 score++; 74 growthCountdown--; 75 76 return score; 77 } 78};
Comments