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.
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample Answer
Requirements
- Input:
grid(m x n) andk >= 0. - Output: a new grid where the element at flat index
imoves 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)....