Amazon Interview Question
SDE1sCountry: United States
#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);
}
}
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;
}
}
}
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);
}
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;
}
}
}
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--;
}
}
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();
}
}
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