Microsoft Interview Question for Software Engineer in Tests






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

int[] retArray SumColumns(int[] array, int numRows){
int n = array.Length;
if (n % numRows != 0)
  throw new Exception("Invalid parameters");

 int columns = n / numRows;
 int[] result = new int[columns];
 
 for (int i = 0 ; i < columns; ++i) 
  for (int j = 0; j < numRows; ++j)
   result[i] += array[i + columns * j];

 return result;
}

Time Complexity: O(columns * numRows) = O(n);
Space Complexity: O(columns) - the result array

- S February 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

perfect

- Anonymous February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

S is always perfect!

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

Wow.. Awesome S

- Anonymous March 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The simplest and correct solution.
Let there be 'n' elements in single dimentional array 'A'.
Let the no of columns be 'noOfColumns'. In this question, noOfColumns = 2.

for(i=0;i<n;i++)
{
    int column = i%noOfColumn;
    result[column]+=A[i];
}

Complexity - 0(n)

- gavinashg March 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

an example of numrows=2 is the following array or u can call it a matrix:

3 4 7 2
2 6 0 9
-------
5 10 7 11 -> resultant one D array returned by the subroutine.

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

Given k rows and an array of size n; we want to add every (n/k)th element of the array

for every element 'i' in array
        array[i%k] += array[i]
// not the first k elements of the array is the required output
-> Time: O(n), Space: O(1)

- GekkoGordan February 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

my solution..has bugs..but the core logic works..

int[] retArray SumColumns(int[] array, int numRows)
{

if(numRows<=0||numRows==1||numRows>=8||array.length<=0)

return array;

int arrLength=array.length();

bool isMatrix=false;

if(arrLength>numRows)// check if this can be assumed a valid matrix
{
if(arrLenght%numRows==0)
isMatrix=true;
else
isMatrix=false;

}

if(isMatrix)
{


int col=floor(arrrLen/numRows);

int[] resultArray=new int[col];

int temp1=0;
int temp2=0;

//currentIndex = numcols * currentrow + current column
//resultArray[currentCol] = array[numcols * currentrow + current column]
for(int currentRow=0;i<numRows;i++)
for(int currentCol=0;j<col;j++)
{

{
resultArray[currentCol]=array[numcols * currentrow + current column]
}
}

}

}

- Rash.Mansur February 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int* ArrayColSum (int*pAray, int numRow, int size)
{

int numOfCol = size/numRow;
int* pColSum = (int*)malloc (numOfCol * sizeof (int));
int i = 0;
int j = 0;
int count = 0;
memset (pColSum, 0x00, numOfCol);
while (j < numOfCol)
{
while (i < size)
{
pColSum [j] += pAray [i];
i = i + numOfCol;
}
j++;
i = j;
}
return pColSum;
}

complexity = (size/numOfCol)*numOfCol = size
O(size)

- Tulley February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] retArray SumColumns(int[] array, int numRows)
{
   // you need array length also
   int numCols = len(array)/numRows;
   int *sum = malloc(sizeof(int)*numCols);
   int count = 0;
   for(int i = 0; i < len; i++)
   {

      int col = i%numCols;
      sum[col] = a[i]
   }
   return sum;
}

- Messi February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int[] retArray(int[] arr, int numR)
{
int [] ret = new int[arr.Length/numR];
int addon = arr.Length / numR;
int k =0;
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(k % addon);
ret[k % addon] = ret[k % addon] + arr[i];
k++;
}
return ret;
}

- kc February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

my bad....an extra print statement which i omitted:
public static int[] retArray(int[] arr, int numR)
{
int [] ret = new int[arr.Length/numR];
int addon = arr.Length / numR;
int k =0;
for (int i = 0; i < arr.Length; i++)
{
ret[k % addon] = ret[k % addon] + arr[i];
k++;
}
return ret;
}

- Anonymous February 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why are you using two counters(i & k). It can be done using only i.

public static int[] retArray(int[] arr, int numR)
{
int [] ret = new int[arr.Length/numR];
int addon = arr.Length / numR;
for (int i = 0; i < arr.Length; i++)
{
       ret[i % addon] = ret[i % addon] + arr[i];
}
return ret;
}

- Anonymous February 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int[] SumOfCols(int[] a, int rows)
        {
            if (rows < 2 || (a.Length % rows != 0))
                throw new Exception("invalid parameters");
            int cols = a.Length / rows;
            int[] result = new int[cols];

            for (int i = 0; i < a.Length; i++)
            {
                int index = i % cols;
                result[index] += a[i];
            }
            return result;
        }

- nsun March 11, 2011 | 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