Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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;

	}

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

Take the transpose of the matrix and reverse elements row-wise

class Rotate90 {
	
	public static void reverseElementsRowWise(int[][] matrix) {
		int n = matrix.length;
		for(int i = 0; i < n; ++i) {
			for(int j = 0; j < n / 2; ++j) {
				int temp = matrix[i][n - j - 1];
				matrix[i][n - j - 1] = matrix[i][j];
				matrix[i][j] = temp;
			}
		}
	}
	
	public static void transpose(int[][] matrix) {
		int n = matrix.length;
		for(int i = 0; i < n; ++i) {
			for(int j = i + 1; j < n; ++j) {
				int temp = matrix[i][j];
				matrix[i][j] = matrix[j][i];
				matrix[j][i] = temp;
			}
		}
	}
	
	public static void rotate90(int[][] matrix) {
		transpose(matrix);
		reverseElementsRowWise(matrix);
	}
	
	public static void print(int[][] matrix) {
		int n = matrix.length;
		for(int i = 0; i < n; ++i) {
			for(int j = 0; j < n; ++j) {
				System.out.print(matrix[i][j]);
				System.out.print(' ');
			}
			System.out.println();
		}
	}
	
	public static void main(String[] args) {
		int[][] matrix = {
				{1, 2, 3, 4},
				{5, 6, 7, 8},
				{9, 10, 11, 12},
				{13, 14, 15, 16}};
		System.out.println("before");
		print(matrix);
		rotate90(matrix);
		System.out.println("after");
		print(matrix);
	}
}

- smffap December 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome! works...
How did you know? I totally bombed this q in the interview.

- someone December 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution

- kevingrice@google.com December 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good solution, you can remove start and use i instead..

- swathi December 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good solution . could you please explain the concept behind this? To rotate to 180 do we need to rotate to 90 two times?

- nmc December 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you :)

The idea is to see how the elements change position. Since this is rotation, the elements have to change in a sane way without jumping around. Looked at it and figured it out.

@nmc : Yes. To rotate the array 180 degrees, call rotate90(matrix) twice.

@swathi: The variable start has been removed.

Cheers.

- smffap December 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@smffap

For 180, just reverseElementsRowWise and reverseElementsColumnWise no transpose needed.

For 270 degrees, you need to reverseElementsColumnWise after transpose.

- Anonymous December 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Found a super explanation here:

rajendrauppal.blogspot.com/2011/12/rotating-2d-array-of-integers-matrix-by.html

- mag December 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void rotateSquareMatrixby90(int [][]sa){

for (int i = 0; i < sa.length-1; i++) {
int t = sa[i][sa.length-1];
sa[i][sa.length-1] = sa[0][i];
int t1 = sa[sa.length-1][sa.length-1-i];
sa[sa.length-1][sa.length-1-i] = t;
t = sa[sa.length-1-i][0];
sa[sa.length-1-i][0] = t1;
sa[0][i] = t;
}

}

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

13 9 5 1
14 6 7 2
15 10 11 3
16 12 8 4

- nmc December 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

result looks wrong, esp. 2nd and 3rd column.

- someone December 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry This is wrong :)

static void rotateSquareMatrixby90(int [][]sa){


for (int j = 0; j < (sa.length/2); j++)
for (int i = j; i < sa.length-1-j; i++) {
int t = sa[i][sa.length-1-j];
sa[i][sa.length-1-j] = sa[j][i];
int t1 = sa[sa.length-1-j][sa.length-1-j-(i-j)];
sa[sa.length-1-j][sa.length-1-j-(i-j)] = t;
t = sa[sa.length-1-j-(i-j)][j];
sa[sa.length-1-j-(i-j)][j] = t1;
sa[j][i] = t;
}

}

This might work

- nmc December 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

package t1;

import java.awt.Point;

public class M3x {
final int n;
int[][] m;
M3x(int n){
this.n =n;
}
void fill(){
m =new int[n][n];
int num = 0;
for (int i=0;i<m.length;i++){
int[] row = m[i];
for (int j = 0; j < row.length; j++) {
row[j]=++num;
}
}
}
void print(){
for (int i=0;i<m.length;i++){
int[] row = m[i];
for (int j = 0; j < row.length; j++) {
System.out.printf("%d\t", row[j]);
}
System.out.println();
}
for (int i=0;i<n;i++)
System.out.print("=======");
System.out.println();
}
void rotate(){
Point p = new Point();
int maxRows = m.length/2 ;

for (int i=0;i<maxRows;i++){
p.y = i;
int[] row = m[i];
for (int j = i; j < row.length-i-1; j++) {
p.x = j;
int v= m[p.y][p.x];
for (;;){
rotatePoint(p);
int cur = m[p.y][p.x];
m[p.y][p.x] = v;
if (p.y==i && p.x==j)
break;
v =cur;
}
}
}
}

void rotatePoint(Point p){
int x = n-p.y - 1;
int y = p.x;
p.x = x;
p.y = y;

}
public static void main(String[] args) {
M3x m3x =new M3x(9);
m3x.fill();
m3x.print();
m3x.rotate();
m3x.print();
}
}

- stanimir December 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void rotate90(int[][] matrix) {
transpose(matrix);
reverseElementsRowWise(matrix);
}

This is enough:
===============
Rest should be trivial

- Anonymous December 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Following method will give the required rotational functionality.

private static int[][] rotateMatrix(int[][] matrix) {
        int[][] rotatedMatrix = new int[matrix.length][matrix.length];
        int len = matrix.length - 1;
        for (int i = 0; i <= len; i++) {
            for (int j = 0; j <= len; j++) {
                rotatedMatrix[j][len - i] = matrix[i][j];
            }
        }
        return rotatedMatrix;
    }

AnanthaNag KUNDANALA

- AnanthaNag KUNDANALA December 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Following is the full class implementation ...

public class Main {

    public static void main(String argc[]) {
        int matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12},{13,14,15,16}};
        int[][] newMatrix = rotateMatrix(matrix);
        display(newMatrix);
    }

    private static int[][] rotateMatrix(int[][] matrix) {
        int[][] rotatedMatrix = new int[matrix.length][matrix.length];
        int len = matrix.length - 1;
        for (int i = 0; i <= len; i++) {
            for (int j = 0; j <= len; j++) {
                rotatedMatrix[j][len - i] = matrix[i][j];
            }
        }
        return rotatedMatrix;
    }

    private static void display(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println("");
        }
    }
}

AnanthaNag KUNDANALA

- AnanthaNag KUNDANALA December 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

requires extra n*n memory though. the question doesn't say 'in-place' but probably it'd be looked upon.

- stanimir December 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think in-place rotation will make more sense...for (i,j), the next place is (m-j+1,n-i+1), and we know there are four elements will swap their position: a->b->c->d->a. Follow these two rules, it's easy to do the in-place rotation.

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

This can be solved in O(n2) time and without using extra space...same question is mentioned in Cracking the Coding Interview.....

- coding for fun December 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Replacement for any x,y
x,y => y, X-x
y, X-x => X-x, X-y
X-x, X-y => y, X-x
y, X-x => x,y

Do the replacement only for first quarter.

- Saurabh February 22, 2015 | 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