Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

Following is for rotating in 90 degree clockwise:

1. Transpose the matrix
2. Reverse each row

Psedocode:

Transpose
    for i in [0, n)
        for j in [0, n)
            if ( i < j )
                swap( M[i][j], M[j][i] )

Reverse a row (rowidx)
    start = 0
    end = cols - 1
    while ( start < end ) {
        swap( M[rowidx][start], M[rowidx][end] )
        ++start
        --end
    }

Rotate
    Transpose
    for i in [0, rows)
        Reverse( i )

- ama March 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Smart solution. Liked ama's logic

- sriniatiisc October 06, 2012 | Flag
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.
0
of 0 vote

#include<stdio.h>

int main()
{
	int a[10][10], b[10][10];
	int i, j, k, l, row, col;
	printf("\nEnter the number of rows and columns\n");
	scanf("%d%d", &row, &col);
	printf("\nEnter the values\n");
	for(i = 0; i <= row - 1; i++)
		for(j = 0; j <= col - 1; j++)
			scanf("%d", &a[i][j]);
	for(i = 0; i <= row - 1; i++)
	{
		for(j = 0; j <= col - 1; j++)
			printf("%d\t", a[i][j]);
		printf("\n");
	}
	
	for(i = 0, k = row - 1; i <= row - 1; i++, k--)
		for(j = 0, l = 0; j <= col - 1; j++, l++)
			b[l][k] = a[i][j];
	
	printf("\n\n");
	
	for(i = 0; i <= col - 1; i++)
	{
		for(j = 0; j <= row - 1; j++)
			printf("%d\t", b[i][j]);
		printf("\n");
	}
	return 0;

}

- Prathamesh March 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Lets modify this question a bit, how can we do the same in by being more efficient in terms of space

- Mohit Ahuja March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Buy the book by Gayle. Its got the solution. Still solve it before looking at the solution

- Sandeep Mukherjee March 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am impressed by Ama's logic... Well done Ama!! :)

- sunny leone March 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Ama.. you approach is correct but just want to improve the way you have transpose the n *n matrix.

for($i=0;$i<$nrow;$i++){
    for($j=$i;$j<$nrow;$j++){
        $temp=$twoDArray[$i][$j];
        $twoDArray[$i][$j]=$twoDArray[$j][$i];
        $twoDArray[$j][$i]=$temp;
        echo "vishwa\n";
    }
}

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

Works for square matrix. Will need additional array to store the intermediate transpose.

- AV April 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Works for square matrix. Will need additional array to store the intermediate transpose.

- AV April 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

awesome approach.. amma its woow.. :)

- time May 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int[][] rotateMatrix(int[][] x){
int[][] m = new int[x[0].length][x.length];
for(int i=0; i<x[0].length; i++){
for(int j=0; j<x.length; j++){
// System.out.format("%4d",m[i][j]=x[j][x[0].length-1-i]); //rotate by 90 degree anticlockwise.
System.out.format("%4d",m[i][j]=x[x.length-1-j][i]); //rotate by 90 degree clockwise.
}
System.out.println();
}
return m;
}

- zoro's hub July 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void cyclic_rotate(int *a, int *b, int *c, int *d)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = *c;
    *c = *d;
    *d = temp;
}

void rotate(int a[][SIZE])
{
    int i, j;
    for(i=0; i<(SIZE/2); i++)
    {
        for(j=0; j<(SIZE+1)/2; j++)
        {
            cyclic_rotate(&a[i][j], &a[SIZE-1-j][i], &a[SIZE-1-i][SIZE-1-j], &a[j][SIZE-1-i]);
        }
    }
}

- Abhijeet Muneshwar January 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suppose there are k rows.

After rotation through 90 , we'll have k colums.
For Clock wise 90 rotation ,
i th row will become (k-i)th column

For Anti-Clockwise , ith row will become ith column

- Sumit March 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The very easy algorithm for rotation of the matrix by 90 degrees we will be following the algorithm given below:

1. We will first find the transpose of the matrix.
2. We will then reverse the columns or swap them and we will finally get the rotated matrix.

Implementation:

void transpose(int mat[R][C], int R, int C){
	for(int i = 0; i < R; i++){
		for(int j = i; j < C; j++){
			swap(mat[i][j], mat[j][i];
		}
	}
}
void swapp(int mat[R][C], int R, int C){
	for(int i = 0; i < C; i++){
		for(int j = 0, k = R - 1; j < k; i++, k--){
			swap(mat[j][i], mt[k][i]);
		}
	}

- swapnilkant11 July 23, 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