Dsa
Matrix
Medium
Matrix Spiral Rotate
Given an m x n matrix, rotate it in a spiral manner by one layer. The outermost layer of the matrix should move inward one step in a clockwise direction, while the inner layers remain unchanged.
Input format:
- The first line contains two integers m and n (1 <= m, n <= 100), the number of rows and columns.
- The next m lines each contain n integers separated by spaces.
Output format:
- The rotated matrix, printed row by row.
Example:
Input:
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
5 1 2 3
9 6 7 4
13 10 11 8
14 15 12 16
Key concepts
matrixrotationspiral
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