Shift 2D Grid

EasyMatrixsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an m x n integer matrix grid and an integer k. One **shift** moves every element one step forward in row-major order: grid[i][j] moves to grid[i][j+1], the last element of a row moves to the first column of the next row, and the very last element grid[m-1][n-1] wraps around to grid[0][0]. Apply the shift k times and return the resulting grid (same dimensions).

Examples

Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[9,1,2],[3,4,5],[6,7,8]]
Every value moves one slot forward and 9 wraps to the front.
Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
With 4 columns, four shifts move every row down by one.
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
Output: [[1,2,3],[4,5,6],[7,8,9]]
Nine shifts on nine cells bring every value back to where it started.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 50
  • -1000 <= grid[i][j] <= 1000
  • 0 <= k <= 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems