Shift a Seating Chart Grid Forward by K Seats in Reading Order

Last updated: November 29, 2026

Quick Overview

Given an m x n grid and an integer k, shift every element k positions forward in row-major reading order, wrapping the last element of the grid around to the first cell, and return the new grid.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Given an m x n grid and an integer k, shift every element k positions forward in row-major reading order, wrapping the last element of the grid around to the first cell, and return the new grid.

Typical of the short timed problems in a Google online assessment. It checks whether you map a 2D index to a 1D position and back, and whether you reduce k modulo the grid size instead of simulating k single steps.

What the Interviewer Expects
  • Maps (r, c) to index r * n + c and back with divmod
  • Reduces k modulo m * n
  • Builds the output in one pass
  • Handles k = 0 and k larger than the grid
  • States O(mn) time
Key Topics to Cover
Index Mapping
Modular Arithmetic
Matrix Traversal
Rotation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Shift in place with O(1) extra space.
  • Shift each row independently by a different amount.
  • Shift backward when k is negative.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: grid (m x n) and k >= 0.
  • Output: a new grid where the element at flat index i moves to flat index (i + k) mod (m*n).
  • k may be far larger than the number of cells....
High-Level Design

Treat the grid as one array of length N = m*n laid out row by row. An element at (r, c) has flat index i = r*n + c; its destination is j = (i + k) % N, which converts back with divmod(j, n)....


Submit Your Answer
Markdown supported