Rotate a Crate of Stones Clockwise and Let Gravity Settle Them
Last updated: November 29, 2026
Quick Overview
A crate is an m by n grid of '#' stones, '*' fixed obstacles, and '.' empty cells. Rotate the crate 90 degrees clockwise and let stones fall straight down until they land on an obstacle, another stone, or the floor; return the resulting n by m grid.
0
0
0 solved
A crate is an m by n grid of '#' stones, '*' fixed obstacles, and '.' empty cells. Rotate the crate 90 degrees clockwise and let stones fall straight down until they land on an obstacle, another stone, or the floor; return the resulting n by m grid.
An online assessment style grid simulation that Google uses to check whether candidates simplify before transforming. The neat insight is to apply gravity along each row first (stones slide right toward what will become the floor) and then rotate.
What the Interviewer Expects
- Realize falling after rotation equals sliding right before rotation
- Process each row from right to left with a landing pointer
- Reset the landing pointer at each obstacle
- Rotate with the correct index mapping: new[j][m-1-i] = old[i][j]
- Run in O(m * n)
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
- What if the crate rotates counter-clockwise instead?
- How would you animate stones falling one cell per frame?
- What if stones can also roll diagonally off obstacles?
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: list of strings or char lists, m rows by n columns. After a clockwise rotation, the right side of each original row becomes the bottom of the new column. Obstacles do not move. Output the r...
High-Level Design
Step 1, gravity: for each row, scan from right to left with land, the rightmost empty slot a stone can reach. At *, set land to the cell left of it. At #, move the stone to land (swap if dif...