NVIDIA Interview Question for Software Engineer / Developers






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

private static int[][] rotateMatrixBy180Degree(int[][] matrix, int n) {
		// TODO Auto-generated method stub
		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.
1
of 1 vote

<pre lang="" line="1" title="CodeMonkey6461" class="run-this">public class RotateBy180Degree {

public static void rotateIt(int[][] arr) {
int temp;
for (int row1 = 0, row2 = arr.length - 1; row1 <= row2; ++row1, --row2) {
for (int col1 = 0, col2 = arr[0].length - 1; col1 < arr[0].length; ++col1, --col2) {
if (row1 == row2 && col1 >= col2) {
break;
}
temp = arr[row1][col1];
arr[row1][col1] = arr[row2][col2];
arr[row2][col2] = temp;
}
}
}

public static void main(String[] args) {
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
int[][] arr2 = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
for (int[] row : arr) {
System.out.println(Arrays.toString(row));
}
RotateBy180Degree.rotateIt(arr);
System.out.println();
for (int[] row : arr) {
System.out.println(Arrays.toString(row));
}
System.out.println();

for (int[] row : arr2) {
System.out.println(Arrays.toString(row));
}
RotateBy180Degree.rotateIt(arr2);
System.out.println();
for (int[] row : arr2) {
System.out.println(Arrays.toString(row));
}
}
}</pre>

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

This is basically to find the transpose of matrix. We can certainly not use the same array as the destination array as given in solution above because it can or can not be a square matrix. We create a new matrix with dimensions n*m where n is the number of columns and m is the number of rows in original matrix.

- Anonymous April 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The solution above is not limited to handle square matrix at all.

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

Hi,
I think Transpose of a matrix is not turning the matrix by 180 degrees. Its rotating the matrix by 90 degrees Hence we dont need a new array. So, if a matrix is

1 2 3 4 5
6 7 8 9 10

It becomes,

10 9 8 7 6
5 4 3 2 1

- Arav May 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fyi, rotating a matrix by 90 degrees in either direction is not the same as taking the transpose of a matrix For instance,

let A =
1 2
3 4

A transpose =
1 3
2 4

A rotated clockwise by 90 =
3 1
4 2

A rotated counterclockwise by 90 =
2 4
1 3

- derp July 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void rotate_180(int m[M][N])
{
for (int i=0; i<M/2; i++)
for (int j=0; j<N; j++)
{
int t = m[i][j];
m[i][j] = m[M-1-i][N-1-j];
m[M-1-i][N-1-j] = t;
}
}

- Anonymous May 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anonymous

this algo looks buggy. It will work correctly when number of rows in even. But if rows are odd, it will not return the correct answer.

e.g.

for input:
{'x','y','z'},
{'p','q','r'},
{'m','n','o'},

the output your algo will give is:
{'o','n','m'},
{'p','q','r'},
{'z','y','x'},

the second row is incorrect, it should be {'r','q','p'},

- harleen June 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

harleen, its ok, solution logic is ok
he simply needs to extend same logic in case if rows (M) is odd
if(M%2){
R_ID=(M+1)/2
for(col=0 to NCOL/2)
swap(a[R_ID][col], a[R_ID][NCOL-col])

simple

- abhityagi85 January 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This works correctly: (written for character array instead of ints)
void rotateBy180(char[r,c])
{
int totalCount = (int)(r * c);
totalCount /= 2;


for (int i = 0; i < r/2; i++)
{
for (int j = 0; j < c; j++)
{
if (totalCount == 0)
break;

char temp = matrix[i, j];
matrix[i, j] = matrix[(int)r - 1 - i, (int)c - 1 - j];
matrix[(int)r - 1 - i, (int)c - 1 - j] = temp;
totalCount--;
}
}
}

- harleen June 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry.. the first for loop should be for (int i = 0; i < r; i++)

- harleen June 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

StringReverse(all rows);
StringReverse(all columns);

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

Awesome. Nice

- Prime July 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

not as simple as you have said ;)

- abhityagi85 January 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Transpose of matrix is as below:-

#include<stdio.h>

int a[2][3]={1,2,3,
4,5,6};
int main()
{
int i,j;
printf("Print the matrix elements are :- \n");
int b[3][2];

for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",a[i][j]);
b[j][i]=a[i][j];
}
printf("\n");

}
printf("Transpose of matrix is as :- \n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",b[i][j]);
}
printf("\n");
}

return 0;
}
Ans :-
Print the matrix elements are :-
1 2 3
4 5 6
Transpose of matrix is as :-
1 4
2 5
3 6

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

int matrix[row][col], temp[col];

//run it for half the rows, it will take care of odd/even cases
for(r=0;r<row/2;r++) 
{
     for(c=0;c<(col-1);c++)
     {
          //copy reverse of last row into an array
          temp[c]=matrix[row-r][col-c];    

          //copy the reverse of rth row into the (row-r)th row
          matrix[row-r][col-c]=matrix[r][c];    

          //copy temp into the rth row from top
          matrix[r][c]=temp[c];    
      }
}

- T January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For 90 degree clockwise rotation, take a transpose and reverse rows. If this is done twice it will give 180 degree clockwise rotation. Complexity O(r*c)

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

package com.app.matrix;

import java.util.Arrays;

public class RotateMatrixBy180Degree {
private static void rotateMatrix(int[][] mat) {
int nRows = mat.length;
int nCols = mat[0].length;

// reverse row
int i = 0;
int j = nRows - 1;
int k = 0;
while (i < j) {
int[] tmp = mat[j];
mat[j] = mat[i];
mat[i] = tmp;
i++;
j--;
}
// reverse column
i = 0;
while(i < nRows) {
int[] row = mat[i];
k = 0;
j = nCols - 1;
while(k < j) {
int tmp = row[k];
row[k] = row[j];
row[j] = tmp;
k++;
j--;
}
i++;
}
}

public static void main(String[] args) {
int[][] mat = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
rotateMatrix(mat);
System.out.println(Arrays.deepToString(mat));
}

}

- karmakar.tanaya October 19, 2019 | 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