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;
}

- red September 11, 2012 | Flag Reply
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.

- lucifer September 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It would work.

- Ashupriya September 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- umer javaid October 04, 2013 | Flag
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..

- saurabh September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous September 22, 2012 | Flag
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*[4];
for(int i=0;i<4;i++)
arr[i]=new int[4];

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);
}

- saurabh September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- saurabh September 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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)

- Sumeet September 11, 2012 | Flag
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.

- AnotherGatorwhofacedsamequestion September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- rajeshanji8 September 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- laterGator September 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- AnotherGatorwhofacedsamequestion September 12, 2012 | Flag
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.

- Anonymous September 18, 2012 | Flag Reply
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.

- Anonymous February 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you please explain me with a sample code..

- Anji February 17, 2013 | Flag


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