Microsoft Interview Question
Country: United States
@Anonymous: Even "better than" does not make sense. Again, I suggest you read the definition of BigOh.
If you want to say better than quadratic, then the right way to say it is o(n^2). We are using SmallOh here, which is quite different from bigOh.
What about the boundary cells. Can we have them as sink?
Because if we don't then your algorithm will fail.
I think this algorithm will perform 2*n steps in the worst case. I think this is O(n).
1, 2, 4, 7, 11,
3, 5, 8, 12, 17,
6, 9, 13, 18, 22,
10, 14, 19, 23, 25,
15, 20, 24, 26, 27;
If you have above matrix, and pick the right bottom element to start, then the search will take n+n comparisons.
The logic does not look at all elements, so it has got to do better than n^2.
#include <cstdio>
const int N = 5;
const int M = 5;
int a[N][M] = {1, 12, 3, 1, -23,
7, 9, 8, 5, 6,
4, 5, 6, -1, 77,
7, 0, 35, -2, -4,
6, 83, 1, 7, -6};
int direct[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
void findLocalMin(int& resRow, int& resCol)
{
int curRow = N / 2;
int curCol = 0;
int curMin = a[curRow][curCol];
for(int i = 0; i < M; i++)
{
if (a[curRow][i] < curMin)
{
curMin = a[curRow][i];
curCol = i;
}
}
bool flag = false;
while(true)
{
flag = true;
for (int i = 0; i < 4; i++)
{
int tempRow = curRow + direct[i][0];
int tempCol = curCol + direct[i][1];
if (tempRow < N &&
tempCol < N &&
a[curRow][curCol] > a[tempRow][tempCol])
{
curMin = a[tempRow][tempCol];
curRow = tempRow; curCol = tempCol;
flag = false;
break;
}
}
if (flag)
{
resRow = curRow;
resCol = curCol;
return;
}
}
};
int main()
{
int resRow, resCol;
findLocalMin(resRow, resCol);
printf("%d", a[resRow][resCol]);
return 0;
}
here we have few points to consider we are not checking the elemnts in the boundaries
1)first we find the rows to be checkd
2)take an array of size number of rows which contains the info. as which rows are checkd
here is the idea with rough code..it may have mistakes suggest me if am wrong anywhere
#define rowno 5
static int count=0;//counts number of such elemnts
int d[r0wno];//array to keep track of rows checked
for(int i=0;i<rowno;i++)
d[i]=0;
findrows(int a[][],int start,int end)
{
int mid=(start+end)/2;
check(a,mid);
if((mid+1)!=(rowno-1) && a[mid+1]!=1)//checking if its last row
{
a[mid+1]=1;
check(a,mid+1);
}
if((mid-1)!=0 && a[mid-1]!=1)//checking if its first row
{
a[mid-1]=1;
check(a,mid-1);
}
findrows(a,start,mid-1);
findrows(a,mid+1,end);
}
check(a[][],int ind)
{
//start from 2nd column because first is boundary
for(int j=1;j<rowno-2;j++)
{
if(a[ind][j]<a[ind][j-1] && a[ind][j]<a[ind[j+1])
{
if(a[ind][j]<a[ind-1][j] && a[ind][j]<a[ind+1][j])
{
count++;
printf("found element %d",a[ind][j]);
}
}
}
}
sry there a few mistakes in my previous code..modified code is
here we have few points to consider we are not checking the elemnts in the boundaries
1)first we find the rows to be checkd
2)take an array of size number of rows which contains the info. as which rows are checkd
here is the idea with rough code..it may have mistakes suggest me if am wrong anywhere
#define rowno 5
static int count=0;//counts number of such elemnts
int d[r0wno];//array to keep track of rows checked
for(int i=0;i<rowno;i++)
d[i]=0;
findrows(int a[][],int start,int end)
{
int mid=(start+end)/2;
check(a,mid);
if((mid+1)!=(rowno-1) && d[mid+1]!=1)//checking if its last row
{
d[mid+1]=1;
check(a,mid+1);
}
if((mid-1)!=0 && dmid-1]!=1)//checking if its first row
{
d[mid-1]=1;
check(a,mid-1);
}
findrows(a,start,mid-1);
findrows(a,mid+1,end);
}
check(a[][],int ind)
{
//start from 2nd column because first is boundary
for(int j=1;j<rowno-2;j++)
{
if(a[ind][j]<a[ind][j-1] && a[ind][j]<a[ind[j+1])
{
if(a[ind][j]<a[ind-1][j] && a[ind][j]<a[ind+1][j])
{
count++;
printf("found element %d",a[ind][j]);
}
}
}
}
//call findrows(a[][],0,rowno-1);
1)it should also check the mid
after check(a,mid);
a[mid]=1;
2)in check function in the for loop condition is j<=rowno-2
int main()
{
int a[5][5]=
{
10, 2, 4, 7, 11,
3, 5, 8, 12, 17,
6, 9, 13, 18, 22,
10, 14, 19, 23, 25,
15, 20, 24, 26, 27
};
int i=0,j;
while(1)
{
for(j=0;j<=4;j++)
{
if(i==0 || j==0)
{
if(a[i][j]<a[i+1][j] && a[i][j]<a[i][j+1])
{
printf("i=%d j=%d\n",i,j);
getch();
}
}
else
{
if(a[i][j]<a[i+1][j] && a[i][j]<a[i][j+1] && a[i][j]<a[i-1][j] && a[i][j]< a[i][j-1])
{
printf("i=%d j=%d\n",i,j);
getch();
}
}
}
i++;
if(i==5)
break;
}
}
This can be done in O(n) time. If we divide the matrix into 4 equal sub-matrices, one of them must contain such (i, j). We can identify that sub-matrix by finding the smallest elements of the middle row and middle column. If this element satisfies the condition, then output it. Otherwise, it has a smaller neighbor. We continue to detect on the sub-matrix that contain this smaller neighbor. The running time is 2n + n + n/2 ... = 4n.
public static int[] findLocalMimMatrix(int[][] matrix, int startRow, int endRow){
int[] ret = new int[2];
int midRow = startRow + (endRow - startRow) / 2;
int rowMin = matrix[midRow][0];
ret[0] = midRow;
ret[1] = 0;
// Get the minimum value of row (startRow + endRow ) / 2
for(int i = 1; i < matrix[midRow].length; i ++){
if(matrix[midRow][i] < rowMin){
rowMin = matrix[midRow][i];
ret[0] = midRow;
ret[1] = i;
}
}
int upNbr = Integer.MAX_VALUE, downNbr = Integer.MAX_VALUE;
if(midRow - 1 >= startRow){
upNbr = matrix[midRow - 1][ret[1]];
}
if(midRow + 1 <= endRow){
downNbr = matrix[midRow + 1][ret[1]];
}
// Find a local minimum
if(rowMin <= upNbr && rowMin <= downNbr){
return ret;
}else{
// Shrink the matrix into half.
if(upNbr >= downNbr){
ret = findLocalMimMatrix(matrix, midRow + 1, endRow);
}else{
ret = findLocalMimMatrix(matrix, startRow, midRow - 1);
}
return ret;
}
}
The time complexity is O(N*lgN)
Less than O(n^2) makes no sense. Go read up on the definition of BigOh.
- Anonymous August 23, 2012As to an approach: here is a greedy approach: Pick a random element and pick the neighbour which is the smallest and less than it. (If there is no such neigbour, you found it). Repeat the process with the neighbour.
You will end up at some 'sink'. Not sure if the time complexity would be sub-quadratic, perhaps we can prove it...