Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrix, algorithm interview question

This was one of my interview questions.

We have a matrix containing integers (no range provided). The matrix is randomly populated with integers. We need to devise an algorithm which finds those rows which match exactly with a column(s). We need to return the row number and the column number for the match. The order of of the matching elements is the same. For example, If, i'th row matches with j'th column, and i'th row contains the elements - [1,4,5,6,3]. Then jth column would also contain the elements - [1,4,5,6,3]. Size is n x n. My solution:

RCEQUAL(A,i1..12,j1..j2)// A is n*n matrix
if(i2-i1==2 && j2-j1==2 && b[n*i1+1..n*i2] has [j1..j2])
   use brute force to check if the rows and columns are same.
if (any rows and columns are same)
   store the row and column numbers in b[1..n^2].//b[1],b[n+2],b[2n+3].. store row no,
                                                 // b[2..n+1] stores columns that 
                                                 //match with row 1, b[n+3..2n+2] 
                                                 //those that match with row 2,etc..

else
   RCEQUAL(A,1..n/2,1..n/2);
   RCEQUAL(A,n/2..n,1..n/2);
   RCEQUAL(A,1..n/2,n/2..n);
   RCEQUAL(A,n/2..n,n/2..n);

Takes O(n^2). Is this correct? If correct, is there a faster algorithm?

like image 367
Brahadeesh Avatar asked Mar 23 '11 16:03

Brahadeesh


People also ask

Is Matrix important for interview?

If the hiring manager uses interview matrix scoring, they have a specific and consistent set of questions to ask, which helps guide the interviewee. Hiring managers can use the scoring sheet to structure the interview and help the candidate concentrate on a structured set of questions.

What is matrix in data structure?

What is Matrix in Data Structure? A matrix represents a collection of numbers arranged in an order of rows and columns. It is necessary to enclose the elements of a matrix in parentheses or brackets.


2 Answers

you could build a trie from the data in the rows. then you can compare the columns with the trie.

this would allow to exit as soon as the beginning of a column do not match any row. also this would let you check a column against all rows in one pass.

of course the trie is most interesting when n is big (setting up a trie for a small n is not worth it) and when there are many rows and columns which are quite the same. but even in the worst case where all integers in the matrix are different, the structure allows for a clear algorithm...

like image 91
Adrien Plisson Avatar answered Sep 28 '22 08:09

Adrien Plisson


You could speed up the average case by calculating the sum of each row/column and narrowing your brute-force comparison (which you have to do eventually) only on rows that match the sums of columns.

This doesn't increase the worst case (all having the same sum) but if your input is truly random that "won't happen" :-)

like image 42
corsiKa Avatar answered Sep 28 '22 07:09

corsiKa