# Matrix Interview Questions

Posted on :06-04-2016

Q1. Given a 2D array of digits, try to find the occurrence of a given 2D pattern of digits. For example, consider the following 2D matrix:

7283455864
6731158619
8988242643
3839505324
9509505813
3843845384
6473530293
7053106601
0834282956
4607924137

Assume we need to look for the following 2D pattern:

950
384
353

Q2. Allocate a 2-D array of size m*n using malloc(). The array should be accessible as a[i][j].

Q3. You are tasked with defining and implementing a function. as input, you are given an n x m matrix. x may appear any number of times in a matrix. your function should modify the b matrix such that any row and column where x originally appears are completely over written with x

For example:
- - - - -
- - - - -
- - - x -
x - - - -
- - - - -

Expected output:
x - - x -
x - - x -
x x x x x
x x x x x
x - - x -

Q4. Write a function to print unique rows of a matrix.

Q5. Write a utility function that takes the starting position (P0) and length (L0) of one line segment plus the start position (P1) and length (L1) of a second line segment and returns the configurations where both segments end at the same point. Both starting points can be anywhere in three dimensional space.

Q6. A 2D matrix is given to you. Now user will give 2 positions (x1,y1) and (x2,y2),  which is basically the upper left and lower right coordinate of a rectangle formed within the matrix. You have to print sum of all the elements within the area of rectangle in O(1) running time.  Now you can do any pre computation with the matrix.  But when it is done you should answer all your queries in constant time.

Example : consider this 2D matrix
1 3 5 1 8
8 3 5 3 7
6 3 9 6 0

Now consider 2 points given by user (0, 2) and (2, 4).
i.e., the enclosed area is
5 1 8
5 3 7
9 6 0

Q7. Given is a matrix arr[n][n], find a submatrix sub[m][m] such summation of all the elements in submatrix is maximum.

Given condition,
1. m <= n
2. m >= 2
3. consider positive, negavive and zero integers in arr[n][n]
4. User provide n.

Q8. Search in a row wise and column wise sorted matrix.

Q9. Write a code to read and write a matrix in below given way.
Note : no. indicates the sequence here.

I/P
1 2 3
4 5 6
7 8 9

O/P
1 6 7
2 5 8
3 4 9

Q10. Given an n x n matrix A(i,j) of integers, find maximum value A(c,d) - A(a,b) over all choices of indexes such that both c > a and d > b.
Required complexity: O(n^2)

Q11. String [] [] matrix = {{"A","N", "L", "Y", "S"},{"I", "S", "D", "E", "S"},{"I", "G", "N", "D", "E"}};

2. Given a word "DES"
3. Write a program to find the occurences of this word "DES". Letters must be next to each other in the matric.
4. "Next" means: left, right, up, down, left down, right down, upper left, upper right
5.. For example: S at (1,1): A, N, L, D, N, G, I, I are next to S at (1,1)

Required output
// // D - [1, 2], E - [1, 3], S- [1, 4]
// D - [1, 2], E - [1, 3], S- [0, 4]
// D - [2, 3], E - [2, 4], S - [1, 4]
// D - [2, 3], E - [1, 3], S - [0, 4]
// D - [2, 3], E - [1, 3], S - [1, 4]

Q12. Given an NxM (N rows and M columns) integer matrix with non-negative values (0..MAX_INT inclusive). What is the maximum sum from going top left (0, 0) to bottom right (N-1, M-1) ? The condition is that when youre at point (p, q), you can only move to either right (p, q+1) or down (p+1, q).

Expected time complexity O(N*M)
Expected space complexity O(N+M)

Q13. There is a binary tree of size N. All nodes are numbered between 1-N(inclusive). There is a N*N integer matrix Arr[N][N], all elements are initialized to zero. So for all the nodes A and B, put Arr[A][B] = 1 if A is an ancestor of B (NOT just the immediate ancestor).

Q14. We are given a matrix of MxN elements with each element either being 0 or 1.Find the shortest path between a given source cell to a destination cell.
An element value of 0 means we cannot create a path out of that cell.

Q15. Consider there are n matrices. For eg, A, B, C and D are four matrices. Find the groupings of matrices during their product, the operations involved in your choice of grouping is minimal.

For eg, you can group like (AB)CD or (ABC)D or A(BC)D or A(BCD) .... But among these options in which grouping the operations of matrix multiplication will be minimal. Remember in matrix multiplication , multiplication and sum of elements are involved.

Q16. Given a matrix of 0s and 1s find the number of groups of 1s in the matrix.

A group of 1s can be formed if a 1 is present either vertically or horizontally to the adjacent 1 and not diagonally.

1 0 0 0
1 1 0 0
0 0 1 1
0 0 1 1

The above matrix has two groups of 1s while the one shown here has only one group

1 1 0 0
1 1 1 0
1 1 0 0

No restrictions on space complexity was given but the interviewer did mention that the time complexity should be efficient and that it should work for extremely large matrixs as well.

Q17. Given a m * n matrix and value k. Find k * k matrix within the given matrix whose sum is maximum.

Q18. Search a number in a large square but sorted matrix. Since the matrix can be very large, keep algorithmic efficiency in mind.

Q19. "Min(Max elements rows) not less than Max(Min of columns)"
can you tell me whether above statement ist true or false..
if it true, then tell me solutoin.
if it false, then tell me example

Q20. In a nxn matrix, data provided like below. We need to find the groups of 1s with the adjusent column and row.

(eg)
0 0 0 0
1 1 1 1
0 0 0 0

group of 1 is 1

1 0 0 0
0 0 0 1
1 1 0 0

group of 1s is 3

Any thought how to get the set of groups.

Q21. Given a binary matrix of N X N of integers , you need to return only unique rows of binary arrays

eg:
0 1 0 0 1
1 0 1 1 0
0 1 0 0 1
1 1 1 0 0

ANS:

0 1 0 0 1
1 0 1 1 0
1 1 1 0 0

Q22. Given a number n... print a spiral matrix in O(1) space example if n=5 the op should be:

25 24 23 22 21
10 09 08 07 20
11 02 01 06 19
12 03 04 05 18
13 14 15 16 17

The question may appear trivial but its not...you have to print in O(1) space

## Matrix Placement Papers

➤ Matrix Technical JAVA Paper

## Related Companies

GE
Geodesic
Fidelity
Grapecity
Soliton
TCE
AT & T
GMR Group
Quinnox

FreshersLive - No.1 Job site in India. Here you can find latest 2018 government as well as private job recruitment notifications for different posts vacancies in India. Get top company jobs for both fresher and experienced. Job Seekers can get useful interview tips, resume services & interview Question and answer. Practice online test free which is helpful for interview preparation. Register with us to get latest employment news/rojgar samachar notifications. Also get latest free govt and other sarkari naukri job alerts daily through E-mail.

 ✖