Amazon Interview Question for Software Engineer / Developers






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

Consider the following code...

int main()
{
  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 i = 0, j=0;
  int rhit = 4, lhit = 0, bhit = 4, uhit = 1;
  int state = 0;
  int counter = 0;

  while(counter < 25)
    {
      if(state == 0)
	{
	  printf("%d ",A[i][j]);
	  j++;
	  if(j>rhit)
	    {
	      j--;
	      i++;
	      rhit--;
	      state=1;
	    }
	  counter++;
	}
      else if(state == 1)
	{
	  printf("%d ",A[i][j]);
	  i++;
	  if(i>bhit)
	    {
	      i--;
	      j--;
	      bhit--;
	      state=2;
	    }
	  counter++;
	}
      else if(state == 2)
	{
	  printf("%d ",A[i][j]);
	  j--;
	  if(j<lhit)
	    {
	      lhit++;
	      j++;
	      i--;
	      state=3;
	    }
	  counter++;
	}
      else if(state == 3)
	{
	  printf("%d ",A[i][j]);
	  i--;
	  if(i<uhit)
	    {
	      uhit++;
	      i++;
	      j++;
	      state=0;
	    }
	  counter++;
	}
    }
}

- Mayank August 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

logic plz

- Anonymous September 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keep traversing the matrix and when you reach the boundary, change direction and keep proceeding. in addition to changing the direction you also knock off the opposite boundary which you have already traversed, keeping in mind not to do so the first time as you haven't yet traversed it.

void printspiralmatrix(int a[HEIGHT][WIDTH])
{
	int n,m;
	n=HEIGHT;
	m=WIDTH;
	int direction = 0; //0 == lt-rt, 1= up to down, 2=rt to left, 3 = down to up 
	int lh = 0, rh = n-1, up = 0, dn = m-1;
	int x=0,y=0;

	while ((x>=lh) && (x<=rh) && (y>=up) && (y<=dn))
	{
		printf("\t%d",a[y][x]);
		switch(direction){
			case 0:
				x++;
				break;
			case 1:
				y++;
				break;
			case 2:
				x--;
				break;
			case 3:
				y--;
				break;
		}
		if ((x== rh) && (y==up))
		{
			direction++;
			 if (y!=0)
				 lh++;
		}
		else if ((x==lh) && (y==dn))
		{
			direction++;
			rh--;
		}
		else if ((y==up) && (x==lh))
		{
			direction++;
			dn--;
		}
		else if ((y==dn) && (x==rh))
		{
			direction++;
			up++;
		}
		direction = direction % 4;
	}
}

int main(int argc, char* argv[])
{
	int a[HEIGHT][WIDTH]= {{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}};
	printspiralmatrix(a);
	return 0;
}

- Neo September 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Forgot to add the top few lines

#include <stdio.h>

#define	HEIGHT 5
#define WIDTH 5

- Neo September 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SpiralSort {

public static void spiralSort(int[][] arr)
{
for(int i=0;i<arr.length;i++)
{
if(i%2==0)
{
System.out.print("{");
for(int j=0;j<arr[0].length;j++)
{
System.out.print(arr[i][j]+", ");
}
System.out.print("}, ");
}
else
{
System.out.print("{");
for(int j=arr[0].length-1;j>-1;j--)
{
System.out.print(arr[i][j]+", ");
}
System.out.print("}, ");
}
}
}
public static void main(String[] args)
{
int arr[][]={{1,2,3},{4,5,6},{7,8,9}};
spiralSort(arr);
}
}

- y so hard September 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void printZigZag(int[][] A) {
boolean zig = true;
int j=0;
for(int i=0;i<A.length;i++){
for(int k=0;k<A[0].length;k++)
if(zig){
System.out.print(A[i][j++]+ " ");
}else{
System.out.print(A[i][j--]+" ");
}
if(zig)
j--; else j++;

zig= !zig;
System.out.println();

}
}

- saksharhere October 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

NA

- weijiang2009 February 06, 2011 | 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