Skip to content

Practice Cyclically Rotating a Grid

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1914
LeetCode Problem

Cyclically Rotating a Grid

Problem Statement

A grid has an even number of rows and an even number of columns. Its outer perimeter is the first layer; removing that perimeter exposes the next layer. Rotate every layer counterclockwise by k positions, without moving values between layers, and return the resulting grid. Each rotation moves a value to the neighboring position along its own perimeter. Practice approach: For each layer, enumerate its perimeter in clockwise order. A counterclockwise movement of k positions means destination i reads original position (i + k) modulo the perimeter length. Store the original perimeter before writing values back. Processing all layers takes O(m*n) time.

Source: Cyclically Rotating a Grid on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [[40,10],[30,20]], k = 1
Output
[[10,20],[40,30]]

# Example 2

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

Constraints

2 <= m, n <= 50
m and n are even.
1 <= grid[i][j] <= 5000
1 <= k <= 1000000000

How to solve Cyclically Rotating a Grid

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

Each layer is an independent ring, so rotating the grid means rotating each ring's flattened sequence by k modulo that ring's own length; flatten, shift, write back, and the enormous k collapses to less than one perimeter per layer.

Why brute force is not enough

Performing k single-step rotations costs O(k * m * n), hopeless for k up to 10^9. Reducing k modulo one global length is also wrong, because each ring has a different perimeter.

Approach

  1. Layer t spans rows t to m - 1 - t and columns t to n - 1 - t; there are min(m, n) / 2 layers because both dimensions are even.
  2. Flatten a layer by walking its perimeter clockwise: top row left to right, right column downward, bottom row right to left, left column upward, never repeating a corner.
  3. With L = 2 * (height + width) - 4 and s = k mod L, the cell at perimeter position i receives the value that was at position (i + s) mod L, which is a counterclockwise move of s.
  4. Write the shifted list back along the same path, reading from the saved copy so overwritten cells never feed later positions, and repeat for every layer; values never cross between layers.

Walkthrough of Example 2

grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 2 → [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]]

  1. Outer layer clockwise from the top-left: 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5; L = 12 and 2 mod 12 = 2.
  2. Shifting left by 2 gives 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 1, 2: the top row becomes 3, 4, 8, 12, the right column continues 16, 15, 14, the bottom row reads 13, 9, 5 from right to left, and the left column reads 1, 2 from bottom to top.
  3. Inner layer clockwise from row 1, column 1: 6, 7, 11, 10; L = 4 and 2 mod 4 = 2, giving 11, 10, 6, 7 at (1,1), (1,2), (2,2), (2,1).
  4. The result is [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]].

Time and space

Time O(m n) · Space O(m + n). m and n are the grid dimensions; each cell belongs to one layer and is read and written once, and only one layer's flattened list of at most 2(m + n) values is held at a time.

Interview pitfalls

  • Taking k modulo a single length for all layers; the outer ring of a 4 by 4 grid has 12 cells while the inner ring has 4.
  • Visiting corners twice when enumerating the perimeter, which corrupts both the length and the alignment.
  • Rotating clockwise by mistake; with a clockwise enumeration, counterclockwise movement means destination i reads from position i + s.
  • Writing into the grid while still reading from it instead of from a saved copy.

Follow-up questions interviewers ask

  • How would you handle odd dimensions where the innermost layer degenerates to a single row, column, or cell?
  • Can you rotate each layer in O(1) extra space with a reversal-based rotation along the perimeter?

Related LeetCode Problems

Practice more problems similar to Cyclically Rotating a Grid.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.