The Rotating the Box interview question gives you a 2D grid representing a box filled with stones (#), empty spaces (.), and obstacles (*). Stones fall to the right due to gravity (before rotating). After applying gravity, rotate the box 90 degrees clockwise. Return the resulting grid. This combines physics simulation with matrix rotation.
This problem is asked at Uber, Microsoft, Meta, Roblox, Amazon, Google, and Capital One because it chains two operations: a physics simulation (gravity) and a matrix transformation (rotation). Both individually are standard interview problems — "settle stones" and "rotate matrix." Combining them tests composability of algorithms and attention to sequential transformations. It has applications in game physics, cellular automata, and material simulation.
The pattern is two-phase processing. Phase 1 — Apply gravity (rightward): for each row, use a two-pointer approach. Place a settle pointer at the rightmost column. Scan left to right (actually right to left): when a stone is found, place it at the settle position and clear its original position, then decrement settle. Reset settle when an obstacle is encountered (stones can't pass through obstacles). Phase 2 — Rotate 90° clockwise: apply the standard transpose-then-reverse-each-row technique.
Grid:
['#', '.', '.', '*']
['#', '#', '.', '.']
Phase 1 — Gravity (stones fall right within each row, blocked by *):
* at col 3 is obstacle. Stone at col 0 → settles at col 2. Result: ['.', '.', '#', '*'].['.', '.', '#', '#'].Grid after gravity:
['.', '.', '#', '*']
['.', '.', '#', '#']
Phase 2 — Rotate 90° CW (transpose + reverse rows): Result:
['.', '.']
['.', '.']
['#', '#']
['*', '#']
For the Rotating the Box coding problem, the array, matrix, and two-pointer interview pattern applies in two phases. Code each phase as a separate function for clarity. Interviewers at Meta and Google appreciate modular code: a apply_gravity(box) function and a rotate_90_cw(box) function, then compose them. Practice the gravity simulation on a row with an obstacle in the middle — it's the trickiest edge case.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Number of Flips to Make Binary Grid Palindromic II | Medium | Solve | |
| Candy Crush | Medium | Solve | |
| Remove Duplicates from Sorted Array II | Medium | Solve | |
| Number of Subarrays with Bounded Maximum | Medium | Solve | |
| Valid Tic-Tac-Toe State | Medium | Solve |