Amazon Interview Question: print the matrix in spiral fo... | Glassdoor

Interview Question

Software Development Engineer Intern Interview(Student Candidate) Seattle, WA

print the matrix in spiral format (inside out). Not that

  difficult, but challenging if you have not heard of the question before (as was the case with me)
Answer

Interview Answer

1 Answer

0

public static void printMatrix(int[][] matrix, int rows, int col)
    {
        int rowStart=0;
        int rowEnd=rows-1;
        int colStart=0;
        int colEnd=col-1;

        while(colStartcolStart;i--)
                System.out.println(matrix[rowEnd][i]);

            for(int i=rowEnd;i>rowStart;i--)
                System.out.println(matrix[i][colStart]);
            rowStart++;
            colEnd--;
            rowEnd--;
            colStart++;

        }

Piyush on Feb 7, 2016

Add Answers or Comments

To comment on this, Sign In or Sign Up.