Dsa
Matrix
Easy
Matrix Spiral Print
Given a 2D matrix of integers, print its elements in spiral order, starting from the top-left corner and moving clockwise around the matrix.
Input format:
- The first line contains two integers, M and N, the dimensions of the matrix.
- The next M lines each contain N integers representing the matrix elements.
Output format:
- A single line containing the integers in spiral order, separated by spaces.
Example:
Input:
3 4
1 2 3 4
5 6 7 8
9 10 11 12
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Key concepts
matrixspiral traversaloutput formatting
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now