## Amazon Interview Question

Country: United States

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

``````private static int[][] rotateMatrixBy90Degree(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 - offset][first];
matrix[last - offset][first] = matrix[last][last - offset];
matrix[last - offset][last] = matrix[i][last];
matrix[i][last] = top;
}
}
System.out.println("Matrix After Rotating 90 degree:-");
printMatrix(matrix, n);
return matrix;

}``````

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

Does it mean "Transpose of a matrix" ?

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

Assuming a (n x n ) matrix
This is simple solution but not necessarily intuitive ...
The steps are for a clockwise rotation:
1. Transpose the matrix
2. Swap column i with column (n-i)

Reason - When you rotate a matrix (let's say clockwise) you are moving the first row to last column , second row to second last column and likewise
and when you transpose a matrix , you are moving the first row to first column , second to second and likewise
so all you have to do after transposing the matrix is just swap the first column with last and second with second last and so on
Time Complexity - O(n2)

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

First of all the Matrix has to be nxn (square) M[n][n]

This can be done in a simpler repetition of the step for outermost to innermost ring (n/2 rings)

``````Last_Row = n -1;
Last_Column = n -1;
First_Row = 0;
First_Column = 0;
while (Last_Row > FirstRow){
FOR(j = First_column; i <= Last_Column; j++){ // can use row or column
temp = M[Last_Row][j];
M[Last_Row][j] = M[j][Last_Column];
M[j][Last_column] = M[First_Row][j];
M[First_Row][j] = M[j][First_column];
M[j][First_column] = temp;
} // end ring iteration

// move to inner ring
First_Row++;
First_column++;
Last_Row --;
Last_column--;

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

``````int a[5][5] = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15}, {16,17,18,19,20}, {21,22,23,24,25} };

int n = 5;
for(int i=0; i<n/2; i++) {
for(int j=0; j<(n+1)/2; j++) {
int temp = a[i][j];
a[i][j] = a[n-1-j][i];
a[n-1-j][i] = a[n-1-i][n-1-j];
a[n-1-i][n-1-j] = a[j][n-1-i];
a[j][n-1-i] = temp;
}
}``````

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

These solutions here are way out of whack. You only need a simple nested loop to flip the matrix. I am using 3x3 matrix here.

``````public class FlipMatrix {

public static void main(String[] args) {
int [][] matrix = new int [3][3];

int start = 23;
for(int i = 0; i < 3; i ++) {
for(int j = 0; j < 3; j++) {
matrix[i][j] = start++;
}
}

printMatrix(matrix);

// flip
for(int i = 0; i < 3; i ++) {
for(int j = 0; j < i; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix [j][i];
matrix [j][i] = temp;
}
}

System.out.println("\n\n");
printMatrix(matrix);

}

static void printMatrix(int [][] matrix) {
for(int i = 0; i < 3; i ++) {
for(int j = 0; j < 3; j++) {
System.out.print(matrix[i][j] + " ");
}

System.out.println("");
}
}
}``````

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

The output of above is

``````23 24 25
26 27 28
29 30 31

23 26 29
24 27 30
25 28 31``````

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.