3248. Snake in Matrix LeetCode Solution
In this guide, you will get 3248. Snake in Matrix LeetCode Solution with the best time and space complexity. The solution to Snake in Matrix problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Snake in Matrix solution in C++
- Snake in Matrix solution in Java
- Snake in Matrix solution in Python
- Additional Resources

Problem Statement of Snake in Matrix
There is a snake in an n x n matrix grid and can move in four possible directions. Each cell in the grid is identified by the position: grid[i][j] = (i * n) + j.
The snake starts at cell 0 and follows a sequence of commands.
You are given an integer n representing the size of the grid and an array of strings commands where each command[i] is either “UP”, “RIGHT”, “DOWN”, and “LEFT”. It’s guaranteed that the snake will remain within the grid boundaries throughout its movement.
Return the position of the final cell where the snake ends up after executing commands.
Example 1:
Input: n = 2, commands = [“RIGHT”,”DOWN”]
Output: 3
Explanation:
0
1
2
3
0
1
2
3
0
1
2
3
Example 2:
Input: n = 3, commands = [“DOWN”,”RIGHT”,”UP”]
Output: 1
Explanation:
0
1
2
3
4
5
6
7
8
0
1
2
3
4
5
6
7
8
0
1
2
3
4
5
6
7
8
0
1
2
3
4
5
6
7
8
Constraints:
2 <= n <= 10
1 <= commands.length <= 100
commands consists only of "UP", "RIGHT", "DOWN", and "LEFT".
The input is generated such the snake will not move outside of the boundaries.
Complexity Analysis
- Time Complexity: O(|\texttt{commands}|)
- Space Complexity: O(1)
3248. Snake in Matrix LeetCode Solution in C++
class Solution {
public:
int finalPositionOfSnake(int n, vector<string>& commands) {
const unordered_map<string, pair<int, int>> directions = {
{"UP", {-1, 0}},
{"RIGHT", {0, 1}},
{"DOWN", {1, 0}},
{"LEFT", {0, -1}}};
int i = 0;
int j = 0;
for (const string& command : commands) {
const auto& [dx, dy] = directions.at(command);
i += dx;
j += dy;
}
return i * n + j;
}
};
/* code provided by PROGIEZ */
3248. Snake in Matrix LeetCode Solution in Java
class Solution {
public int finalPositionOfSnake(int n, List<String> commands) {
Map<String, int[]> directions = Map.of("UP", new int[] {-1, 0}, //
"RIGHT", new int[] {0, 1}, //
"DOWN", new int[] {1, 0}, //
"LEFT", new int[] {0, -1});
int i = 0;
int j = 0;
for (final String command : commands) {
int[] direction = directions.get(command);
i += direction[0];
j += direction[1];
}
return i * n + j;
}
}
// code provided by PROGIEZ
3248. Snake in Matrix LeetCode Solution in Python
class Solution:
def finalPositionOfSnake(self, n: int, commands: list[str]) -> int:
directions = {
"UP": (-1, 0),
"RIGHT": (0, 1),
"DOWN": (1, 0),
"LEFT": (0, -1),
}
i = 0
j = 0
for command in commands:
dx, dy = directions[command]
i += dx
j += dy
return i * n + j
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.