NVIDIA Interview Question for Software Engineer / Developers






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

private static int[][] rotateMatrixBy180Degree(int[][] matrix, int n) {
		for (int layer = 0; layer < n / 2; layer++) {
			int first = layer;
			int last = n - 1 - layer;
			for (int i = first; i < last; i++) {
				int offset = i - first;
				int top = matrix[first][i];
				matrix[first][i] = matrix[last][last - offset];
				matrix[last][last - offset] = top;
				int leftBottom = matrix[last - offset][first];
				matrix[last - offset][first] = matrix[i][last];
				matrix[i][last] = leftBottom;
			}
		}
		System.out.println("Matrix After Rotating 180 degree:-");
		printMatrix(matrix, n);
		return matrix;

	}

- Vir Pratap Uttam May 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i hope the following program works...

for(i=0;i<row_total;i++)
{
  for(j=0;j<column_total;j++)
  {
    c[i][j] = a[i][j];
  }
}
for(i=0;i<row_total;i++)
{
  for(j=0;j<column_total;j++)
  {
    a[i][j] = c[row_total-1-i][colum_total-1-j];
  }
}

- Ace April 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we have to swap the rows first (like 1 and last, 2nd and 2nd last etc), then do similar stuff with columns, this will give a 180 degree effect to the matrix.

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

For the exact question, it might be better to use swap the content, however, maybe a rotation matrix is a more general solution ---- it is normally need transpose also.

Becasue the interview question is from the NVedia, I think rotate matrix might be what they want.

rotate: newx = x*cos(a) - y * sin(a)
newy = x * sin(a) + y * cos(a)

when a is 180degree, newx = -x newy = -y

we first move the origin to the center of the matrix, for an matrix with (m,n) dimension, the center is ((float)m/2, (float)n/2), where we suppose index start from 0; a more easier way is to multiply all the dimension by 2.

Following are the seudo code
for (x = 0; x < m; x++)
{
for (y = 0; y < n; y++)
{
// general case, but need handle the edging
/*
nx = ((m - 2*x)*cosa - (n - 2*y)*sina) >> 1;
ny = ((m - 2*x)*sina + (n - 2*y)*cosa) >> 1;
*/
/*We can simplify the question as m = n*/
N[nx][ny] = O[x][y];
}
}

- GhostArcher September 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Reverse the rows first and then the columns.

- ankit.batra11 April 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Rotate by 90
2. Swap columns
3. Rotate by 90

- Anonymous July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Guys , we are talking about 180 , not 90
say :
a:
0,1,2,3
4,5,6,7

after switching180, it would be:
3,2,1,0
7,6,5,4
so it is basically just a reverse of ever row.

- Charles December 31, 2013 | 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