Rotate a Grid in Row-Major Order

Quick Overview

Rotate a Grid in Row-Major Order. Shift grid to the right k times. Each shift moves an element one column right; the last column continues into the next row's first column, and the final cell wraps to the first cell.

Rotate a Grid in Row-Major Order

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Shift grid to the right k times. Each shift moves an element one column right; the last column continues into the next row's first column, and the final cell wraps to the first cell. Return the resulting grid with the same dimensions. ```cpp class Solution { public: vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k); }; ``` ### Examples **Example 1:** ```text Input: grid = [[1,2],[3,4]], k = 2 Output: [[3,4],[1,2]] Explanation: A two-place cyclic rotation of1,2,3,4 puts3,4 first. ``` **Example 2:** ```text Input: grid = [[5,6,7]], k = 4 Output: [[7,5,6]] Explanation: Four shifts on three cells are equivalent to one shift. ``` ### Constraints - grid has m rows and n columns with `1 <= m,n <= 50`. - Every entry is an integer in `[-1000,1000]`. - k is an integer from 0 through 100. ### Contract considerations Input dimensions and all values are retained; this is one row-major cycle rather than separate row rotations. ```hint Read the boundary condition Where does a last-column value move, and what happens when the final cell is shifted? ``` ### Boundary reminders Input dimensions and all values are retained; this is one row-major cycle rather than separate row rotations.

Overview: Rotate a Grid in Row-Major Order. Shift grid to the right k times. Each shift moves an element one column right; the last column continues into the next row's first column, and the final cell wraps to the first cell.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Shift grid to the right k times. Each shift moves an element one column right; the last column continues into the next row's first column, and the final cell wraps to the first cell. Return the resulting grid with the same dimensions.

class Solution {
public:
    vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k);
};

Examples

Example 1:

Input: grid = [[1,2],[3,4]], k = 2
Output: [[3,4],[1,2]]
Explanation: A two-place cyclic rotation of1,2,3,4 puts3,4 first.

Example 2:

Input: grid = [[5,6,7]], k = 4
Output: [[7,5,6]]
Explanation: Four shifts on three cells are equivalent to one shift.

Constraints

  • grid has m rows and n columns with 1 <= m,n <= 50 .
  • Every entry is an integer in [-1000,1000] .
  • k is an integer from 0 through 100.

Contract considerations

Input dimensions and all values are retained; this is one row-major cycle rather than separate row rotations.

Boundary reminders

Input dimensions and all values are retained; this is one row-major cycle rather than separate row rotations.