Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

input array (a) and flag array are the members of class. Below is the code for counting both zero groups and one groups.

private void CountOneGroups()
    {
        flag = new boolean[a.length][a[0].length];
        for(int i = 0; i < a.length; i++)
            for(int j = 0; j < a[i].length; j++)
                flag[i][j] = false;
        int zeroCount, oneCount;
        zeroCount = 0;
        oneCount = 0;
        for(int i = 0; i < a.length; i++)
            for(int j = 0; j < a[i].length; j++)
                if(flag[i][j] != true)
                {
                    if(a[i][j] == 0)
                        zeroCount++;
                    else
                        oneCount++;
                    FourConnected(i, j, a[i][j]);
                }
        System.out.println("Zero Group Count : " + zeroCount);
        System.out.println("One Group Count : " + oneCount);
    }
    private void FourConnected(int i, int j, int value)
    {
        if(!flag[i][j])
        {
            flag[i][j] = true;
            if(i - 1 >= 0 && a[i - 1][j] == value)
                FourConnected(i - 1, j, value);
            if(j - 1 >= 0 && a[i][j - 1] == value)
                FourConnected(i, j - 1, value);
            if(i + 1 < a.length && a[i + 1][j] == value)
                FourConnected(i + 1, j, value);
            if(j + 1 < a[0].length && a[i][j + 1] == value)
                FourConnected(i, j + 1, value);
        }
    }

- Nani November 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you Nani. It is simple one. I think instread of creating a new boolean 2D Array, we might play around the existing array with different values like -1 for visited 0s and -2 for 1s and count the 1 group. What you think?.

- JobHunter November 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

beautiful thinking.

- baghel November 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if input is
0 0 0 0
1 1 0 1
0 1 1 1

will the group of 1's is 1 or 3??

- Appu November 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The Algo provided by Nani will return 1 in your case, Appu

And I think this is also expected from the question's algo

- Skataria December 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

cool soln!

- ashwin December 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey35139" class="run-this">class Cluster {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] matrix = { { 1, 0, 1 }, { 1, 0, 1 }, { 0, 1, 1 } };
int cluster = dfs(matrix);
System.out.print("Cluster:" + cluster);
}

private static int dfs(int[][] matrix) {
// TODO Auto-generated method stub
int cluster = 0;
printMatrix(matrix);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 1) {
dfsVisit(matrix, i, j);
cluster++;
}
}
}
printMatrix(matrix);
return cluster;
}

private static void dfsVisit(int[][] matrix, int i, int j) {
// TODO Auto-generated method stub
if (i < 0 || i >= matrix.length)
return;
if (j < 0 || j >= matrix[0].length)
return;
if (matrix[i][j] != 1)
return;
matrix[i][j] = -1;
dfsVisit(matrix, i + 1, j);
dfsVisit(matrix, i, j + 1);
dfsVisit(matrix, i - 1, j);
dfsVisit(matrix, i, j - 1);
}

private static void printMatrix(int[][] matrix) {
// TODO Auto-generated method stub
System.out.println();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.printf("%4d", matrix[i][j]);
}
System.out.println();
}

}

}
</pre><pre title="CodeMonkey35139" input="yes">
</pre>

- Anonymous November 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey83180" class="run-this">class Cluster {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] matrix = { { 1, 0, 1 }, { 1, 0, 1 }, { 0, 1, 1 } };
int cluster = dfs(matrix);
System.out.print("Cluster:" + cluster);
}

private static int dfs(int[][] matrix) {
// TODO Auto-generated method stub
int cluster = 0;
printMatrix(matrix);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 1) {
dfsVisit(matrix, i, j);
cluster++;
}
}
}
printMatrix(matrix);
return cluster;
}

private static void dfsVisit(int[][] matrix, int i, int j) {
// TODO Auto-generated method stub
if (i < 0 || i >= matrix.length)
return;
if (j < 0 || j >= matrix[0].length)
return;
if (matrix[i][j] != 1)
return;
matrix[i][j] = -1;
dfsVisit(matrix, i + 1, j);
dfsVisit(matrix, i, j + 1);
dfsVisit(matrix, i - 1, j);
dfsVisit(matrix, i, j - 1);
}

private static void printMatrix(int[][] matrix) {
// TODO Auto-generated method stub
System.out.println();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.printf("%4d", matrix[i][j]);
}
System.out.println();
}

}

}
</pre><pre title="CodeMonkey83180" input="yes">
</pre>

- Anonymous November 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Look at connected component labeling problem
en.wikipedia.org/wiki/Connected-component_labeling

- chennavarri November 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int FindAdjacentOns(int** matrix, int rows, int cols)
    {
	int groupCount = 0;

        for (int i=0; i< rows ; i++)
        {
            for(int j=0; j< cols ; j++)
            {
                if(matrix[i][j] == 1)
                {
	                groupCount ++;
	                CheckAdjacent( matrix,  i, j)
                }
            }
        }
        return groupCount;
    }

    void CheckAdjacent(int** matrix, int i, int j, int rows, int cols)
    {
        if (matrix != null && matrix[i][j] == 0)
        {
            return;
        }
        matrix[i][j] = 0;

        if (i > 0)
        {
            CheckAdjacent(matrix, i - 1, j, rows, cols);
        }
        if (j > 0)
        {
            CheckAdjacent(matrix, i - 1, j, rows, cols);
        }
        if (i < rows - 1)
        {
            CheckAdjacent(matrix, i + 1, j, rows, cols);
        }
        if (j < cols - 1)
        {
            CheckAdjacent(matrix, i, j + 1, rows, cols);
        }
    }

- satishl November 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't understand the question completely?
Do i need to find out group of 1 or 0 in rows?
Please explain?

- sathishperias November 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is it calculation of group of 1's sequentially ? or group 1's in all directions?

- Krishna November 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Need to find the closet 1s. While finding the 1s we can move up/down.left and right. There is no diagonal move for finding group of 1s

- nmarasu November 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If some one can provide 0(n) complexity solution for this?

- ashu November 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int  count  = 0;
for(int row = 1; row < m; row++) {
    for(int col = 0; col < n; col++) {
        if((A[row-1][col] == 1) && (A[row][col] == 0)) {
            count++; // This is a move from 1 to 0.
        }
    }
}
return 0;

So whenever we transition from 1 to 0 we increment the count.

- Anonymous November 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Running C++ code:

#include<iostream>
using namespace std;
void numGroups(int a[][5],int i,int j,int n,int curr )
{

if(i < 0 || j < 0 )
return;
if((i == n) || (j == n))
return;


if(a[i][j]==1)
{

a[i][j] = curr;

numGroups(a,i-1,j,n,curr);
numGroups(a,i+1,j,n,curr);

numGroups(a,i,j-1,n,curr);
numGroups(a,i,j+1,n,curr);

numGroups(a,i+1,j+1,n,curr);
numGroups(a,i-1,j+1,n,curr);

numGroups(a,i+1,j-1,n,curr);
numGroups(a,i-1,j-1,n,curr);

}
}

bool discoverAll(int arr[][5],int n,int &next_i,int &next_j)
{
for(int i =0;i<n;i++)
for(int j=0;j<n;j++)
if(arr[i][j]==1)
{
next_i = i;
next_j = j;
return false;
}
return true;
}

int numGroupsTrigger(int arr[][5],int n)
{
int groupCount = 2;
int start_i=0,start_j=0;
while(!discoverAll(arr,n,start_i,start_j))
{

numGroups(arr,start_i,start_j,n,groupCount);
groupCount++;

}

return groupCount-2;
}
void showArr(int arr[][5],int n)
{
for(int i =0;i<n;i++)
{

for(int j=0;j<n;j++)
cout<<arr[i][j]<<" ";

cout<<endl;
}
}
int main()
{
int array2D[5][5] = { {1,0,1,0,1},
{0,1,1,0,0},
{1,1,1,1,1},
{0,0,1,1,0},
{1,0,1,0,1}
};
int n = 5;

cout<<"# 1s: "<<numGroupsTrigger(array2D,5);
cout<<endl;
showArr(array2D,n);
cout<<endl;
char c;
cin>>c;
return 0;
}

- Ratna Kumar November 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming that I am writing code for a 4 by 4 matrix.

static void GroupCount(ref int[,] Arr)
        {
            int groupCount = 1;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (Arr[i, j] == 1)
                    {
                        groupCount += 1;
                        Arr[i, j] = groupCount;
                        if (i + 1 < 4)
                        {
                            if (Arr[i + 1, j] == 1)
                            {
                                Arr[i + 1, j] = Arr[i, j];
                                CombineGroup(ref Arr, i + 1, j);
                            }
                        }
                        if (j + 1 < 4)
                        {
                            if (Arr[i, j + 1] == 1)
                            {
                                Arr[i, j + 1] = Arr[i, j];
                                CombineGroup(ref Arr, i, j + 1);
                            }
                        }
                    }
                }
            }

            Console.WriteLine("GroupCount is {0}", groupCount-1);
            Console.ReadLine();
        }

        private static void CombineGroup(ref int[,] Arr, int i, int j)
        {
            if (i + 1 < 4)
            {
                if (Arr[i + 1, j] == 1)
                {
                    Arr[i + 1, j] = Arr[i, j];
                    CombineGroup(ref Arr, i + 1, j);
                }
            }
            if (j + 1 < 4)
            {
                if (Arr[i, j + 1] == 1)
                {
                    Arr[i, j + 1] = Arr[i, j];
                    CombineGroup(ref Arr, i, j + 1);
                }
            }
        }

- anonymous July 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming that I am working on a 4by4 matrix

static void GroupCount(ref int[,] Arr)
        {
            int groupCount = 1;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (Arr[i, j] == 1)
                    {
                        groupCount += 1;
                        Arr[i, j] = groupCount;
                        if (i + 1 < 4)
                        {
                            if (Arr[i + 1, j] == 1)
                            {
                                Arr[i + 1, j] = Arr[i, j];
                                CombineGroup(ref Arr, i + 1, j);
                            }
                        }
                        if (j + 1 < 4)
                        {
                            if (Arr[i, j + 1] == 1)
                            {
                                Arr[i, j + 1] = Arr[i, j];
                                CombineGroup(ref Arr, i, j + 1);
                            }
                        }
                    }
                }
            }

            Console.WriteLine("GroupCount is {0}", groupCount-1);
            Console.ReadLine();
        }

        private static void CombineGroup(ref int[,] Arr, int i, int j)
        {
            if (i + 1 < 4)
            {
                if (Arr[i + 1, j] == 1)
                {
                    Arr[i + 1, j] = Arr[i, j];
                    CombineGroup(ref Arr, i + 1, j);
                }
            }
            if (j + 1 < 4)
            {
                if (Arr[i, j + 1] == 1)
                {
                    Arr[i, j + 1] = Arr[i, j];
                    CombineGroup(ref Arr, i, j + 1);
                }
            }
        }

- anonymous July 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this solution assumes the input matrix order is 4by4.

static void GroupCount(ref int[,] Arr)
        {
            int groupCount = 1;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (Arr[i, j] == 1)
                    {
                        groupCount += 1;
                        Arr[i, j] = groupCount;
                        if (i + 1 < 4)
                        {
                            if (Arr[i + 1, j] == 1)
                            {
                                Arr[i + 1, j] = Arr[i, j];
                                CombineGroup(ref Arr, i + 1, j);
                            }
                        }
                        if (j + 1 < 4)
                        {
                            if (Arr[i, j + 1] == 1)
                            {
                                Arr[i, j + 1] = Arr[i, j];
                                CombineGroup(ref Arr, i, j + 1);
                            }
                        }
                    }
                }
            }

            Console.WriteLine("GroupCount is {0}", groupCount-1);
            Console.ReadLine();
        }

        private static void CombineGroup(ref int[,] Arr, int i, int j)
        {
            if (i + 1 < 4)
            {
                if (Arr[i + 1, j] == 1)
                {
                    Arr[i + 1, j] = Arr[i, j];
                    CombineGroup(ref Arr, i + 1, j);
                }
            }
            if (j + 1 < 4)
            {
                if (Arr[i, j + 1] == 1)
                {
                    Arr[i, j + 1] = Arr[i, j];
                    CombineGroup(ref Arr, i, j + 1);
                }
            }
        }

- varunpvk July 24, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More