# Top 10 Matrix Coding Problems for Programming interviews

Matrix is one of the under-rated topic for coding interviews but Matrix coding problems are not that easy to solve and  In this article, we'll see some of the popular Matrix coding problems for coding interviews. I actually learned a lot by solving Matrix related problems. I still remember the first Matrix based problem I solve was about how to multiply two matrices in Java and I learned a lot about multi-dimensional array and nested loop in Java by solving that problem. The second problem I took was about transposing Matrix and that was also quite challenging for me at that time but helped me to further solidify my knowledge about loops and array in Java.

Every since then I have always looked for interesting Matrix based coding problems and today, I am going to share a couple of gems with you. You can use them to test your programming and coding skills.

By the way, If you are preparing for coding interview or looking for coding problems just want to improve your programming skills then you have come to the right place. In the past, I have shared coding problems on array, linked list, binary tree, dynamic programming, system design, recursion, and data structures in general and in this article I will cover Matrices.

## 10 Matrix based Coding Problems for Practices

Without wasting anymore of your time, here is a list of Matrix based coding problems for practice. You can use these Matrix coding questions to not only prepare for programming job interviews but also for improving your coding skills. I have included both beginner, medium and difficult level Matrix questions in this list. Let me know how many you an solve without getting any help on Internet.

### 1. Vertical Flip Matrix Problem

Given an m x n 2D image matrix where each integer represents a pixel, write a method flipVerticalAxis to flip it in-place along its vertical axis.

Examples
[[1, 0],
[1, 0]]
->
[[0, 1],
[0, 1]]

### 2. Horizontal Flip Matrix Problem

Given an m x n 2D image matrix where each integer represents a pixel, write a method flipHorizontalAxis to flip it in-place along its horizontal axis.

Examples
[[1, 0],
[0, 1]]
->
[[0, 1],
[1, 0]]

### 3. Transpose a Given Matrix [Solution]

You are given a square 2D image matrix where each integer represents a pixel. Write a method transposeMatrix to transform the matrix into its transpose - in-place. The transpose of a matrix is a matrix which is formed by turning all the rows of the source matrix into columns and vice-versa…

Examples
[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 0, 1, 2],
[3, 4, 5, 0]]
->
[[1, 5, 9, 3],
[2, 6, 0, 4],
[3, 7, 1, 5],
[4, 8, 2, 0]]

### 4. Rotate a given Matrix in Java

Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method rotate to rotate the image by 90 degrees in place.

Examples
[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 0, 1, 2],
[3, 4, 5, 6]]
->
[[3, 9, 5, 1],
[4, 0, 6, 2],
[5, 1, 7, 3],
[6, 2, 8, 4]]

### 5. Boggle Search Problem

You’re given a 2D Boggle Board which contains an m x n matrix of chars - char[][] board, and a String - word. Write a method - boggleSearch that searches the Boggle Board for the presence of the input word. Words on the board can be constructed with sequentially adjacent letters, where adjacent letters are horizontal or vertical neighbors (not diagonal). Also, each letter on the Boggle Board must be used only once.

Examples

Input Board :
[
[A, O, L],
[D, E, L],
[G, H, I]
]
Word: "HELLO"
Output: true

### 6. Zero Matrix Problem

Write a method zeroMatrix with an algorithm such that if an element in a MxN matrix is 0, its entire row and column are set to 0. Matrix should be changed in place.

Examples
[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 0, 1, 2],
[3, 4, 5, 0]]
->
[[1, 0, 3, 0],
[5, 0, 7, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]

### 7. Count Paths in Given Matrix

You’re given a game board that has m x n squares on it, represented by an m x n array. Write a method countPaths that takes in m and n and returns the number of possible paths from the top left corner to the bottom right corner. Only down and right directions of movement are permitted.

Examples
countPaths(m = 2, n = 2) => 2

as on the following 2x2 Board, the two paths are A->C->D and A->B->D

A B
C D

### 8. Matrix Max Sum Path Problem

Given an m x n matrix filled with non-negative integers, find the maximum sum along a path from the top-left of the grid to the bottom-right. Return this maximum sum. The direction of movement is limited to right and down.

Examples
[[1, 2, 3],
[4, 5, 6], -> 29 (1 + 4 + 7 + 8 + 9)
[7, 8, 9]]

### 9. Largest Square Matrix Problem

Given a two dimensional matrix made up of 0's and 1's, write a method largestSquare to find the largest square containing all 1’s and return its area.

The area is simply the sum of all integers enclosed in the square.

Example
Input Matrix :

1101 xxxx 11xx
1101 => 11xx or 11xx
1111 11xx xxxx

Output : 4

### 10.  Spiral Matrix Problem

In this problem, you are given an m x n matrix and you need to return all elements of the matrix in spiral order. This is a medium level Matrix problem.

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]

Output: [1,2,3,6,9,8,7,4,5]

Here is an example of Spiral order in Matrix:

That's all about common matrix based coding problems for programmers and developers. You can use these questions to not just prepare for coding interviews but also to improve your programming and coding skills. If you are a beginner then Matrix based problems are good to improve your knowledge about loops and multi-dimensional array. They are really good to improve your coding sense as well.