Amazon Interview Question for SDE1s


Country: United States




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

Assume the matrix has dimention m*n, firstly print the surrounding number, reduce the dimension to (m-1)*(n-1), repeat the first step recursively until one dimension of the matrix reaches zero.

- Shijing.Lv January 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

void printSpiral(int**,int,int,int,int);
int main()
{
int **arr;
int r=5,c=5;
int count=1;
int i,j;



arr = (int**)malloc(r * sizeof(int*));
for (i=0; i<r; i++)
{
arr[i] = (int*)malloc(c * sizeof(int));
}

for(i=0; i<r; i++ ){
for(j=0; j<c; j++){
arr[i][j] = count++;
}
}
printSpiral(arr,0,r-1,0,c-1);

}

void printSpiral(int** arr, int startRow, int endRow, int startColumn, int endColumn){
int r;
int c;
for( r = startRow, c = startColumn; c <= endColumn; c++){
printf(" %d ", arr[r][c]);
}
printf("\n");
for(r=startRow+1, c=endColumn; r<=endRow; r++){
printf(" %d ", arr[r][c]);
}
printf("\n");
for(r=endRow, c=endColumn-1; c>=startColumn;c--){
printf(" %d ", arr[r][c]);
}
printf("\n");
for(r=endRow-1, c=startColumn; r>=startRow+1; r--){
printf(" %d ", arr[r][c]);
}
printf("\n");
if(startRow+1<endRow && startColumn+1<endColumn)
{
printSpiral(arr, startRow+1,endRow-1, startColumn+1,endColumn-1);
}

}

- Suyash Patel January 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Spiral {

/**
* @param args
*/
public static void main(String[] args) {
int arr[][]={{1,2,3,4},{10,11,12,5},{9,8,7,6}};
int T=0,B=2,L=0,R=3;
int dir=0;
while(T<=B && L<=R){
if(dir==0){
for(int i=L;i<=R;i++)
System.out.print(arr[T][i]);
T++;

System.out.println(" ");
}
if(dir==1){
for(int i=T;i<=B;i++)
System.out.print(arr[i][R]);
R--;

System.out.println(" ");
}
if(dir==2){
for(int i=R;i>=L;i--)
System.out.print(arr[B][i]);
B--;
dir++;
System.out.println(" ");
}
if(dir==3){
for(int i=B;i>=T;i--)
System.out.print(arr[i][L]);
L++;

System.out.println(" ");
}
dir=(dir+1)%4;
}

}


}

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

This was my attempt in java:

private static void printSpiral(int[][] matrix, Point place, int width, int length, int iterationCount) {

	/* Top */
	if (iterationCount >= matrix.length * matrix[0].length) { return; } 
	for (int i = 0; i < width; i++) {
		System.out.println(matrix[place.x][++place.y]); /* Increment current column width times */
		iterationCount++;
	}

	/* Right */
	if (iterationCount >= matrix.length * matrix[0].length) { return; } 
	for (int i = 0; i < length -1; i++) {
		System.out.println(matrix[++place.x][place.y]);	/* Increment current row length-1 times */
		iterationCount++;
	}

	/* Bottom */
	if (iterationCount >= matrix.length * matrix[0].length) { return; } 
	for (int i = 0; i < width - 1; i++) {
		System.out.println(matrix[place.x][--place.y]); /* Decrement current column width-1 times */
		iterationCount++;
	}

	/* Left */
	if (iterationCount >= matrix.length * matrix[0].length) { return; } 
	for (int i = 0; i < length - 2; i++) {
		System.out.println(matrix[--place.x][place.y]); /* Decrement current row length - 2 times */
		iterationCount++;
	}

	printSpiral(matrix, place, width-2, length-2, iterationCount);	/* Recurse to next layer */
    }

    public static void printSpiral (int[][] matrix) {
            printSpiral(matrix, new Point(0,-1), matrix[0].length, matrix.length, 0);

    }

- GSheld January 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void spinalPrint(int[][] arr) {
	if (arr == null) {
		return;
	}

	// l, t, r, and b stand for left, top, right, and bottom respectively
	for (int l = 0, t = 0, r = arr[0].length, b = arr.length; l < r && t < b;) {
		for (int col = l; col < r; ++col) {
			System.out.print(arr[t][col] + " ");
		}
		++t;
		--r;
		for (int row = t; row < b; ++row) {
			System.out.print(arr[row][r] + " ");
		}
		if (t < b) {
			--b;
			for (int col = r - 1; col >= l; --col) {
				System.out.print(arr[b][col] + " ");
			}
		}
		if (l < r) {
			for (int row = b - 1; row >= t; --row) {
				System.out.print(arr[row][l] + " ");
			}
			++l;
		}
	}
}

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

Java version: Working for all kinds of test cases:

public static void printMatrix_Spiral_form(int a[][])
      {
          if(a==null)
              return ;
          
         // int column=a.length; 
          int row=a.length-1;  // correct answer
          
          int column=a[0].length-1;  // correct answer
          
          // first printing row then printing column
          int i=0,j=0,k=0,p=0;
          
          while(j<=column && i<=row)
          {
              k=j;
              while(j<=column)
              {
                  System.out.print(a[i][j]+"  ");
                  j++;
              }
              
              i++;
              j--;
              p=i;
              while(i<=row)
              {
                  System.out.print(a[i][j]+"  ");
                  i++;
              }
              
              i--;
              j--;
              while(j>=k)
              {
                  System.out.print(a[i][j]+"  ");
                  j--;
              }
              i--;
              j++;
              while(i>=p)
              {
                       System.out.print(a[i][j]+"  ");
                       i--;
              }
              i++;
              j++;
              
              column--;
              row--;
          }
          
      }

- codeAddict January 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your code will not work when 1 5 1 2 3 4 5 (one dimenstion array)

- neelabh singh February 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't this similar to matrix rotation? Gayle covered it in one of her interview videos.

- copyconstructor January 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void Dspiral()
{
int[,] ipmatrix = new int[4, 4] { { 1, 5, 3, 2 }, { 2, 8, 4, 0 }, { 3, 7, 1, 9 }, { 5, 2, 8, 6 } };
int rowlength = ipmatrix.GetLength(0);
int columnlength = ipmatrix.GetLength(1);
int chk = 0;

for (int i = 0; i < rowlength; i++)
{
for (int j = 0; j < columnlength; j++)
{
Console.Write(ipmatrix[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("Spiralled matrix");

for (int i = 0; i < rowlength; i++)
{
if ((chk % 2) == 0)
{
for (int j = 0; j < columnlength; j++)
{
Console.Write(ipmatrix[i, j]);
}
chk++;
}
else
{
for (int j = columnlength - 1; j >= 0; j--)
{
Console.Write(ipmatrix[i, j]);
}
chk++;
}
Console.WriteLine();
}

}

- vivek828 January 19, 2014 | 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