## Microsoft student Interview Question for Students

Country: United States

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

public int[,] MoveRowsToColumns(int [,]mat , int m, int n)
{
if (m <= 0 || n <= 0)
{
throw new Exception("invalid rows/cols");
}

for (int i = 0; i < m; i++)
{
for (int j = i; j < n; j++)
{
int val = mat[i,j];
mat[i,j] = mat[j,i];
mat[j, i] = val;
}
}

return mat;
}

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

I do not think this will work when the rows and column are of unequal length.

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

It would work.

Comment hidden because of low score. Click to expand.
0

its O(n*n ) not O(n)

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

one could try swapping. swap(a[i][j],a[j][i]). with the symmetricity of the swap you run only for half the matrix...

alternatively if we can use map-reduce...go for it..

Comment hidden because of low score. Click to expand.
0

It doesn't necessarily say that the rows in the file are fixed length or that the file is NxN.

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

// Type your C++ code and click the "Run Code" button!
// Your code output will be shown on the left.
// Click on the "Show input" button to enter input data to be read (from stdin).

#include <iostream>
using namespace std;
int n=4;
void reverse(int,int,int**);
int main() {
// Start typing your code here...
cout << "Hello world!" << endl;
int** arr=new int*;
for(int i=0;i<4;i++)
arr[i]=new int;

int count=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
arr[i][j]=count;
cout<<arr[i][j]<<" ";
count++;
}
cout<<endl;
}

for(int i=0;i<4;i++)
reverse(i,i,arr);

for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
cout<<arr[i][j]<< " ";
}
cout<<endl;
}

return 0;
}

void reverse(int i,int j,int** arr){
int x;
if(j==n)
return;

x=arr[i][j];arr[i][j]=arr[j][i];arr[j][i]=x;
reverse(i,j+1,arr);
}

Comment hidden because of low score. Click to expand.
0

am not sure of the complexity of this code though..I dont think it would be O(n)..

Comment hidden because of low score. Click to expand.
0

What you could do is go over the file character by character i.e a[i][j] and put each character in another file at position a[j][i]. Then replace the original file with the new file. Now you have rows replaced by columns in O(n)

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

If its an array, the task is pretty easy. Following code would do.
{
for (i=0; i<n; i++){
for(j=i; j<n; j++){
swap (array[i][j], array[j][i]);
}
}
}

But its million * million operation and on a FILE.
Here we have to read chunks (say 512*512) from file in an array (buffer), perform swap(row, column) operation on array.
Now we have million/512 chunks in a row and same in a column.
Again we have to apply swap (row * column) on these chunks.

The question was to see if a candidate can scale a small solution to a very big data set which is not easy to handle. Getting exact position or data set(chunk) as we require here from a file can be tricky.

Comment hidden because of low score. Click to expand.
0

i think its not o(n) it may be o(n2)

Comment hidden because of low score. Click to expand.
0

So anotherGator ... U saying there's no O(N) for this ?

Comment hidden because of low score. Click to expand.
0

Yes if its n = million then its O(n^2). That's the file size.
If it helps, total no of swaps is much less than n^2
No other way I can think of.
I was asked to optimize it as much as I can, not with constraint O(n).

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

I think its about scalability.. the most appropriate answer here IMO will be to first ask the interviewer how the data is stored in file. How we can access data..After that if possible get row i and column i , swap...upto n. If we cannot access row and column like that. we need to do this in small fragments .. the question here is not only for swaps in O(n) i think its also about no of file operations.

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

IF O(n) is required - Use a map.

Key => column
Value => Appended data from each row

Split each row and append the column data to the map. After millionth pass, you just have to write them out.

Comment hidden because of low score. Click to expand.
0

can you please explain me with a sample code..

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.

### 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.