Google Interview Question
Data EngineersCountry: United States
bool check(bool mat[][4], int size, int srow, int scol, int h)
{
for (int i = srow; i < srow + h; i++)
for (int j = scol; j < scol + h; j++)
if (mat[i][j] == 1)
return 0;
return 1;
}
int countZeroSq(bool mat[][4], int size, int srow, int scol, int h)
{
if (srow == size - 1 || scol == size - 1|| srow + h > size || scol + h > size)
return 0;
int count = 0;
if (check(mat, size, srow, scol, h))
{
if (h > 1)
count++;
count += countZeroSq(mat, size, srow, scol, h + 1);
}
return count;
}
int countZeroSq(bool mat[][4], int size)
{
int count = 0;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if(mat[i][j] == 0)
count += countZeroSq(mat, size, i, j, 1);
}
}
return count;
}
Algorithm : Iterate over the rows in O(num_row^2) and go through only the col indexes which differs by row indexes. If all are zero, increment the result.
- DyingLizard January 20, 2018