Amazon.com

  www.amazon.com
  www.amazon.com

Interview Question

Software Development Engineer In Test I (SDET) Interview Seattle, WA

You are given a n*n matrix of bits (1s and 0s) where

  1 represents land and 0 represents water. Adjacent 1s can be considered as joined together to form sort of island in water. Count the number of islands. Discuss complexity.
Answer

Interview Answer

1 Answer

0

int nCnt = 0;
int land = 0;
boolean bIsOne = false;
int a[rows][cloumns];
int i,j;
for(i = 0;i < rows ; i++)
{
   for(j = 0; j < columns ; j++ )
   {
        if(a[i][j] == 1)
        {
           bIsOne = true;
             nCnt = nCnt +1;
        }
        else
        {
            if(bIsOne == true )
            {
                   if(nCnt > 1)
                   {
                          land ++;
                    }
                    nCnt = 0;
                    bIsOne = false;
            }
        }
    }
}

Pradnya on Feb 1, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.