Amazon Interview Question
Quality Assurance EngineersCountry: India
Interview Type: In-Person
/* This is for 6x6 Matrix, Similarly you can do for 2x2 Matrix*/
public class SpiralPattern {
public static void main(String[] args) {
int arr[][]={{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, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36}};
int startRow = 0;
int col = arr[0].length;
SpiralPrint(arr,startRow,col);
}
private static void SpiralPrint(int[][] arr, int startRow, int col) {
int i,j;
for(i=startRow;i<arr.length;i++)
{
for(j=0;j<col;j++)
{
if(j==col-1||i==startRow)
System.out.print(arr[i][j]+"\t");
else
System.out.print("\t");
}
System.out.print("\n");
}
if(startRow<arr.length)
SpiralPrint(arr,startRow+1,col-1);
}
}
package com.my.algo.problemsolving;
public class SpiralTraversal {
public static void main(String[] args) {
int arr[][]=
{{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, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36}};
int rowcount = 6;
int colcount = 6;
int total = rowcount * colcount;
int count = 0;
int idx1 = 0;
int idx3 = rowcount - 1;
int idx4 = idx1;
while (count < total) {
for (int i = idx1; i < colcount; i++) {
System.out.print(arr[idx1][i] + " ");
count++;
}
for (int j = idx1 + 1; j <= rowcount - 1; j++) {
System.out.print(arr[j][colcount - 1] + " ");
count++;
}
idx3 = colcount - 1;
for (int k = idx3 - 1; k >= idx1; k--) {
System.out.print(arr[idx3][k] + " ");
count++;
}
idx4 = rowcount - 1;
for (int p = idx4 - 1; p > idx1; p--) {
System.out.print(arr[p][idx1] + " ");
count++;
}
rowcount--;
colcount--;
idx1 = idx1 + 1;
}
}
}
OUTPUT:
-------------
1 2 3 4 5 6 12 18 24 30 36 35 34 33 32 31 25 19 13 7 8 9 10 11 17 23 29 28 27 26 20 14 15 16 22 21
Complexity : O(n) with no additional space
public class SpiralPattern {
public static void main(String[] args) {
int arr[][]={{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, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36}};
int startRow = 0;
int col = arr[0].length;
int row =arr.length;
SpiralPrint(arr,startRow,col, row, 0);
}
private static void SpiralPrint(int[][] arr, int startRow, int col , int row, int loop) {
int i,j ,k, l;
//print first row
if(startRow==arr.length/2)
{
return;
}
for(j=0+loop;j<col-1;j++)
{
System.out.print(arr[startRow][j]+" ");
}
// print last column
for( k=0+loop;k<row-1;k++)
{
System.out.print(arr[k][j]+" ");
}
//print last row
for(l=col-1;l>0+loop;l--)
{
System.out.print(arr[k][l]+" ");
}
//print first column
for(i=row-1;i>0+loop;i--)
{
System.out.print(arr[i][l]+" ");
}
SpiralPrint(arr,startRow+1,col-1, row-1, loop+1);
}
}
public class SpiralPattern {
public static void main(String[] args) {
int arr[][]={{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, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36}};
int startRow = 0;
int col = arr[0].length;
int row =arr.length;
SpiralPrint(arr,startRow,col, row, 0);
}
private static void SpiralPrint(int[][] arr, int startRow, int col , int row, int loop) {
int i,j ,k, l;
//print first row
if(startRow==arr.length/2)
{
return;
}
for(j=0+loop;j<col-1;j++)
{
System.out.print(arr[startRow][j]+" ");
}
// print last column
for( k=0+loop;k<row-1;k++)
{
System.out.print(arr[k][j]+" ");
}
//print last row
for(l=col-1;l>0+loop;l--)
{
System.out.print(arr[k][l]+" ");
}
//print first column
for(i=row-1;i>0+loop;i--)
{
System.out.print(arr[i][l]+" ");
}
SpiralPrint(arr,startRow+1,col-1, row-1, loop+1);
}
}
public class SpiralPattern {
public static void main(String[] args) {
int arr[][]={{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, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36}};
int startRow = 0;
int col = arr[0].length;
int row =arr.length;
SpiralPrint(arr,startRow,col, row, 0);
}
private static void SpiralPrint(int[][] arr, int startRow, int col , int row, int loop) {
int i,j ,k, l;
//print first row
if(startRow==arr.length/2)
{
return;
}
for(j=0+loop;j<col-1;j++)
{
System.out.print(arr[startRow][j]+" ");
}
// print last column
for( k=0+loop;k<row-1;k++)
{
System.out.print(arr[k][j]+" ");
}
//print last row
for(l=col-1;l>0+loop;l--)
{
System.out.print(arr[k][l]+" ");
}
//print first column
for(i=row-1;i>0+loop;i--)
{
System.out.print(arr[i][l]+" ");
}
SpiralPrint(arr,startRow+1,col-1, row-1, loop+1);
}
}
int arr[][]=
{{1, 2, 3, 4, 5, 6,88},
{7, 8, 9, 10, 11, 12,77},
{13, 14, 15, 16, 17, 18,66},
{19, 20, 21, 22, 23, 24,55},
{25, 26, 27, 28, 29, 30,44},
{31, 32, 33, 34, 35, 36,33}};
int rowcount = 6;
int colcount = 7;
int total = rowcount * colcount;
int row=0;
int col=0;
for(int i=1;i<=total;i++){
System.out.println(arr[row][col]);
col=col+1;
if(i%7 ==0 ){
row=row+1;
col=0;
}
}
}
int arr[][]=
{{1, 2, 3, 4, 5, 6,88},
{7, 8, 9, 10, 11, 12,77},
{13, 14, 15, 16, 17, 18,66},
{19, 20, 21, 22, 23, 24,55},
{25, 26, 27, 28, 29, 30,44},
{31, 32, 33, 34, 35, 36,33}};
int rowcount = 6;
int colcount = 7;
int total = rowcount * colcount;
int row=0;
int col=0;
for(int i=1;i<=total;i++){
System.out.println(arr[row][col]);
col=col+1;
if(i%7 ==0 ){
row=row+1;
col=0;
}
}
}
int arr[][]=
{{1, 2, 3, 4, 5, 6,88},
{7, 8, 9, 10, 11, 12,77},
{13, 14, 15, 16, 17, 18,66},
{19, 20, 21, 22, 23, 24,55},
{25, 26, 27, 28, 29, 30,44},
{31, 32, 33, 34, 35, 36,33}};
int rowcount = 6;
int colcount = 7;
int total = rowcount * colcount;
int row=0;
int col=0;
for(int i=1;i<=total;i++){
System.out.println(arr[row][col]);
col=col+1;
if(i%7 ==0 ){
row=row+1;
col=0;
}
}
}
/**
*
*/
package com.nanda.amazon.problems;
/**
* @author Nandakumar 23-May-2017
*
*/
public class TwoDimSpiral {
public static void main(String[] args) {
int[][] array = new int[4][4];
array[0] =new int[] {2,3,4,5};
array[1] =new int[] {6,7,8,9};
array[2] =new int[] {10,11,12,13};
array[3] =new int[] {14,15,16,17};
int m = 4;
int n = 4;
spiralMethod(array, m, n);
}
public static void spiralMethod(int[][] array, int m, int n) {
int top = 0;
int bottom = m - 1;
int right = n - 1;
int left = 0;
int dir = 0;
while (top <= bottom && left <= right) {
if (dir == 0) {
for (int i = top; i <= right; i++) {
System.out.println(array[top][i]);
}
dir = 1;
top++;
}
if (dir == 1) {
for (int i = top; i <= bottom; i++) {
System.out.println(array[i][right]);
}
dir = 2;
right--;
}
if (dir == 2) {
for (int i = right; i >= left; i--) {
System.out.println(array[bottom][i]);
}
dir = 3;
bottom--;
}
if (dir == 3) {
for (int i = bottom; i >= top; i--) {
System.out.println(array[i][left]);
}
dir = 0;
left++;
}
}
}
}
public class SpiralPattern {
- ritu.jnu08 June 11, 2016public static void main(String[] args) {
int arr[][]={{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, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36}};
int startRow = 0;
int col = arr[0].length;
int row =arr.length;
SpiralPrint(arr,startRow,col, row, 0);
}
private static void SpiralPrint(int[][] arr, int startRow, int col , int row, int loop) {
int i,j ,k, l;
//print first row
if(startRow==arr.length/2)
{
return;
}
for(j=0+loop;j<col-1;j++)
{
System.out.print(arr[startRow][j]+" ");
}
// print last column
for( k=0+loop;k<row-1;k++)
{
System.out.print(arr[k][j]+" ");
}
//print last row
for(l=col-1;l>0+loop;l--)
{
System.out.print(arr[k][l]+" ");
}
//print first column
for(i=row-1;i>0+loop;i--)
{
System.out.print(arr[i][l]+" ");
}
SpiralPrint(arr,startRow+1,col-1, row-1, loop+1);
}
}