Google

www.google.com

Interview Question

Software Engineer In Test Interview Hyderābād (India)

Assume a matrix of integers they are sorted in boh row and

  column vice .. how do u find a given number from the matrix in a optimal way?
Answer

Interview Answer

9 Answers

3

Let the matrix is n*m matrix. Then O(n log m) solution is trivial (binary search in each row). There is a easy O(n+m) solution too. The idea is to start from upper right corner (mat[0][m-1]) and traverse toward lower left corner (mat[n-1][0]). On the way check each entry and depending on whether larger go left or down. If there is a solution you will find it on the way. Or you will arrive to a point where you can no longer move without going out of the matrix. Either way you will check at most O(n+m) entries thus the solution in O(n+m).

Anonymous on Apr 15, 2010
1

I think it could be done even better than in O(n+m). Instead of starting at the upper right corner do a binary search on last column and find the biggest element that is still smaller than the given number. Say it's gonna be A[i, m-1]. Now we could throw away all rows up to an including i (since A[i, m-1] is larger than all of these elements) and the last column. Repeat everything for a smaller matrix of size (n - i, m - 1);

dp on May 21, 2010
0

To elaborate a little on dp's idea and add my take on it I would do a binary search on the last column to find the interval where the number is in. This interval will be one row within the matrix (assuming the value is not in the last column) and to find the interval should be O(log n). Then I would do a binary search on the row that remains which should cost O(log m). Combined that would be O(log n) + O(log m). Let me know what you guys think of this solution.

Anonymous on Sep 8, 2010
0

Anonymous, consider the matrix and you are searching for 9. Using this new algorithm we would look at rows 2 and 3 since 8 < 9 < 10 and completely miss 9 in the last row.
(1 3 5 7)
(2 3 6 8)
(3 5 8 10)
(6 9 11 12)

Me on Dec 2, 2010
0

dp,
what do you do if the first element in the last column is already larger than the target? I.e. there may be no number that is smaller that the target. You could throw away that column, but that's it, I think. If so, the worst case becomes O(m).

Ethelbert on Jan 4, 2011
1

What if you did a binary search on the diagonal....

Ethelbert on Jan 4, 2011
0

A better solution than O[log(mn)] is unlikely. An mxn array can be laid in memory as a m*n one dimensional array at its base form. So a matrix sorted by row and column simply means this mxn one dimensional array is sorted. A binary search will get you the result in O[log(mn)], that is the fastest solution available.

Anonymous on Mar 24, 2011
0

How about this.
for m columns - do binary range_search.
As in, start with m/2 th column, if the number is in this range...col(0) <numb<col(n)..

else...if less than col(0), then do binary range_search in 1..m/2 columns
else if greater than col(n), then do binary_range_search in m/2+1...m columns...
at each step, when u found in a range.. do binary search within that column..

Complexity.. for range search we are doing two comparison per column... so we can narrow down to a range in 2*log(m) columns.
and within a column, we do log(n)..
so - finally complexity = 2*log(m)*log(n) -> O (log(m+n))

matrix on Jun 1, 2012
0

oops, there is a catch....
If the number is in a range, it can be in that column, or any of the columns towards left of it....and in some cases, if its in the range.. it can still be on any columns on right.....
need to tweak a bit more.

matrix. on Jun 1, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.