Rotate a Stone Box and Settle Its Contents

Quick Overview

Rotate a Stone Box and Settle Its Contents. boxGrid contains stones #, fixed obstacles *, and empty cells .. Rotate this rectangular box ninety degrees clockwise, then let stones fall downward until blocked by an obstacle, a stone, or the bottom.

Rotate a Stone Box and Settle Its Contents

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem `boxGrid` contains stones `#`, fixed obstacles `*`, and empty cells `.`. Rotate this rectangular box ninety degrees clockwise, then let stones fall downward until blocked by an obstacle, a stone, or the bottom. Obstacles rotate with the box but do not fall. Stones are initially supported. Return the matrix with its dimensions exchanged after settling. ```cpp class Solution { public: vector<vector<char>> rotateTheBox(vector<vector<char>>& boxGrid); }; ``` ### Examples **Example 1:** ```text boxGrid = [["#",".","*","#","."]] Output: [["."],["#"],["*"],["."],["#"]] Explanation: Stones settle separately on the two sides of the obstacle. ``` **Example 2:** ```text boxGrid = [["#","#","."],["#","#","."]] Output: [[".","."],["#","#"],["#","#"]] Explanation: Each original row compacts rightward before mapping the rotation. ``` ### Constraints - `m == boxGrid.length` and `n == boxGrid[i].length`. - `1 <= m,n <= 500`. - Cells are `#`, `*`, or `.`. - Initially every stone rests on an obstacle, another stone, or the bottom. ### Clarifying Questions Rotated gravity corresponds to rightward movement in each original row. Stones do not shift horizontally in the rotated coordinate system. ```hint Inspect the structure Which objects move under gravity after the rotation, and what must happen to the obstacles? ``` ### Reasoning checkpoints - Model the specified clockwise rotation and downward settling without moving an obstacle relative to the box. - Preserve stone multiplicity and show that no stone can fall farther in the final grid. ### Optional extensions How would rotating first and then compacting output columns implement the same behavior?

Overview: Rotate a Stone Box and Settle Its Contents. boxGrid contains stones #, fixed obstacles *, and empty cells .. Rotate this rectangular box ninety degrees clockwise, then let stones fall downward until blocked by an obstacle, a stone, or the bottom.

Google logo
Google
May 30, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

boxGrid contains stones #, fixed obstacles *, and empty cells .. Rotate this rectangular box ninety degrees clockwise, then let stones fall downward until blocked by an obstacle, a stone, or the bottom. Obstacles rotate with the box but do not fall. Stones are initially supported. Return the matrix with its dimensions exchanged after settling.

class Solution {
public:
    vector<vector<char>> rotateTheBox(vector<vector<char>>& boxGrid);
};

Examples

Example 1:

boxGrid = [["#",".","*","#","."]]
Output: [["."],["#"],["*"],["."],["#"]]
Explanation: Stones settle separately on the two sides of the obstacle.

Example 2:

boxGrid = [["#","#","."],["#","#","."]]
Output: [[".","."],["#","#"],["#","#"]]
Explanation: Each original row compacts rightward before mapping the rotation.

Constraints

  • m == boxGrid.length and n == boxGrid[i].length .
  • 1 <= m,n <= 500 .
  • Cells are # , * , or . .
  • Initially every stone rests on an obstacle, another stone, or the bottom.

Clarifying Questions Guidance

Rotated gravity corresponds to rightward movement in each original row. Stones do not shift horizontally in the rotated coordinate system.

Reasoning checkpoints

  • Model the specified clockwise rotation and downward settling without moving an obstacle relative to the box.
  • Preserve stone multiplicity and show that no stone can fall farther in the final grid.

Optional extensions

How would rotating first and then compacting output columns implement the same behavior?