LearnFree QA courses with practical knowledge checks
All questions

Advanced · Java · Matrices

Java: traverse a matrix in spiral order

Java coding interview preparation for QA and SDET roles · ~20 min

This is a Pro program. Practice and solutions require ₹99/month. New coding programs are added every month. Basic programs remain free.

Problem statement

Implement static List<Integer> spiral(int[][] a).

Rectangular matrix; return clockwise spiral traversal starting at top left.

Input & output

spiral(new int[][]{{1,2,3},{4,5,6},{7,8,9}}) → [1, 2, 3, 6, 9, 8, 7, 4, 5]

Interview discussion

  • Explain the time and auxiliary-space complexity of your approach.
  • Which boundary cases matter when testing traverse a matrix in spiral order?

Java 8 · Matrices. Implement the required static method in class Solution. The test runner checks returned values, including boundary cases; printing a sample alone does not solve the exercise.