Amazon Interview Question for Software Engineer / Developers






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

<pre lang="" line="1" title="CodeMonkey65238" class="run-this">#include<stdio.h>
int main()
{
int matrix[10][10];
int r,c;
printf("Enter rows and columns:");
scanf("%d%d",&r,&c);
int m=(r/2)+1;
int i=0;
while (i<=m)
{
int k=i;
int j=0;
for(;j<=c-2;j++)
printf("%d",matrix[k][j]);
for(;k<=r-2;k++)
printf("%d",matrix[k][j]);
for(;j>=i;j--)
printf("%d",matrix[k][j]);
for(;k>i;k--)
printf("%d",matrix[k][j]);
c--;
r--;
}
return 0;
}</pre><pre title="CodeMonkey65238" input="yes">
I think this might work.</pre>

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

{{void printSpiral(int row,int col,int m,int n){
if (row>m && col>n) return;
if (m==n && row==m) System.out.print(arr[row][col]);

int i,j;
for(i=row,j=col;j<n;j++) System.out.print(arr[i][j] + " ");
for(i=row,j=n ;i<m;i++) System.out.print(arr[i][j] + " ");
for(i=m,j=n ;j>col;j--) System.out.print(arr[i][j] + " ");
for(i=m,j=col;i>row;i--) System.out.print(arr[i][j] + " ");

print(row+1,col+1,m-1,n-1,arr);
return;}}}

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

void printSpiral(int row,int col,int m,int n){
   if (row>m && col>n) return;
   if (m==n && row==m) System.out.print(arr[row][col]);

   int i,j;
   for(i=row,j=col;j<n;j++) System.out.print(arr[i][j] + " ");
   for(i=row,j=n ;i<m;i++) System.out.print(arr[i][j] + " ");
   for(i=m,j=n ;j>col;j--) System.out.print(arr[i][j] + " ");
   for(i=m,j=col;i>row;i--) System.out.print(arr[i][j] + " ");

   print(row+1,col+1,m-1,n-1,arr);
   return;
}

- IsAs July 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Have tested, this should work.... suppose this is matrix m*n m<=n

void MatrixSpiral( int matrix[][], int m, int n )
{
	int layer = ceil((double)m/2 );

	for  (i = 0; i < layer; i++ )
	{
		// edge case, last loop
		if (m%2 == 1 && i = layer-1 )
		{
			//loop1 top, left -> right 
			for (j = 0 + i ; j <= n-1-i; j++ )
				cout << matrix[i][j];
			return;
		}
		//loop1 top, left -> right 
		for (j = 0 + i ; j < n-1-i; j++ )
			cout << matrix[i][j];
		// loop2 right, top->bottom
		for( j = 0 +i; j < m-1-i; j++ )
			cout << matrix[j][n-1-i]
		// loop3 bottom, right->left
		for(j = n-1-i; j >i; j-- )
			cout << matrix[m-1-i][j];
		// loop4 left, bottom->top
		for(j = m-1-i;j>i;j--)
			cout << matrix[j][i];
	}
}

- iatbst January 29, 2012 | 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