Rotate a Square Matrix Clockwise While Both Diagonals Stay Fixed

Read the full interview experience this question came from →

Quick Overview

Rotate every element of a square matrix that lies off both diagonals by 90 degrees clockwise a given number of times, leaving the main-diagonal and anti-diagonal values in place. It tests precise index mapping, handling of odd and even matrix sizes, and bug-free simulation under timed assessment conditions.

Rotate a Square Matrix Clockwise While Both Diagonals Stay Fixed

Company: Sig

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

Rotate a square matrix clockwise by 90 degrees a given number of times, except that every element on either diagonal stays where it is. The two diagonals split the remaining cells into four triangular regions (top, right, bottom, and left). One clockwise turn moves each non-diagonal element exactly as an ordinary 90-degree clockwise rotation of the whole matrix would: the top region moves into the right region, the right region into the bottom, the bottom into the left, and the left into the top. Elements on the main diagonal and on the anti-diagonal never move. ### Function Signature `rotate_except_diagonals(matrix: list[list[int]], turns: int) -> list[list[int]]` ### Rules - `matrix` is `n x n`. Cell `(r, c)` (zero-based row and column) is a diagonal cell when `r == c` or `r + c == n - 1`. - One clockwise turn moves the value at every non-diagonal cell `(r, c)` to cell `(c, n - 1 - r)`. Every diagonal cell keeps its value. - Apply exactly `turns` consecutive clockwise turns and return the resulting matrix as an `n x n` list of lists. ### Constraints - `1 <= n <= 100`. - `1 <= turns <= 4`. - Every element is an integer in `[-1000000000, 1000000000]`. ### Examples Input: `matrix = [[1,2,3,4,5],[2,1,9,6,3],[7,0,4,8,1],[5,2,4,1,9],[6,4,3,2,1]], turns = 1` Output: `[[1,5,7,2,5],[4,1,0,6,2],[3,4,4,9,3],[2,2,8,1,4],[6,9,1,3,1]]` The main-diagonal values 1, 1, 4, 1, 1 and the anti-diagonal values 5, 6, 4, 2, 6 stay in place. The non-diagonal values of the first column, 2, 7, 5 from top to bottom, move into the top row at columns 3, 2, and 1, so the top row becomes `1, 5, 7, 2, 5`. Input: `matrix = [[1,2,3],[4,5,6],[7,8,9]], turns = 2` Output: `[[1,8,3],[6,5,4],[7,2,9]]` Input: `matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], turns = 1` Output: `[[1,9,5,4],[14,6,7,2],[15,10,11,3],[13,12,8,16]]` With an even size the diagonals do not share a center cell: the eight diagonal values stay fixed and the other eight rotate.

Overview: Rotate every element of a square matrix that lies off both diagonals by 90 degrees clockwise a given number of times, leaving the main-diagonal and anti-diagonal values in place. It tests precise index mapping, handling of odd and even matrix sizes, and bug-free simulation under timed assessment conditions.

Read the full Sig Software Engineer interview experience this question came from

|Home/Coding & Algorithms/Sig
Sig logo
Sig
Sep 17, 2026
mediumSoftware EngineerOnline AssessmentCoding & Algorithms
0
0

Rotate a square matrix clockwise by 90 degrees a given number of times, except that every element on either diagonal stays where it is.

The two diagonals split the remaining cells into four triangular regions (top, right, bottom, and left). One clockwise turn moves each non-diagonal element exactly as an ordinary 90-degree clockwise rotation of the whole matrix would: the top region moves into the right region, the right region into the bottom, the bottom into the left, and the left into the top. Elements on the main diagonal and on the anti-diagonal never move.

Function Signature

rotate_except_diagonals(matrix: list[list[int]], turns: int) -> list[list[int]]

Rules

  • matrix is n x n . Cell (r, c) (zero-based row and column) is a diagonal cell when r == c or r + c == n - 1 .
  • One clockwise turn moves the value at every non-diagonal cell (r, c) to cell (c, n - 1 - r) . Every diagonal cell keeps its value.
  • Apply exactly turns consecutive clockwise turns and return the resulting matrix as an n x n list of lists.

Constraints

  • 1 <= n <= 100 .
  • 1 <= turns <= 4 .
  • Every element is an integer in [-1000000000, 1000000000] .

Examples

Input: matrix = [[1,2,3,4,5],[2,1,9,6,3],[7,0,4,8,1],[5,2,4,1,9],[6,4,3,2,1]], turns = 1

Output: [[1,5,7,2,5],[4,1,0,6,2],[3,4,4,9,3],[2,2,8,1,4],[6,9,1,3,1]]

The main-diagonal values 1, 1, 4, 1, 1 and the anti-diagonal values 5, 6, 4, 2, 6 stay in place. The non-diagonal values of the first column, 2, 7, 5 from top to bottom, move into the top row at columns 3, 2, and 1, so the top row becomes 1, 5, 7, 2, 5.

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]], turns = 2

Output: [[1,8,3],[6,5,4],[7,2,9]]

Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], turns = 1

Output: [[1,9,5,4],[14,6,7,2],[15,10,11,3],[13,12,8,16]]

With an even size the diagonals do not share a center cell: the eight diagonal values stay fixed and the other eight rotate.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...