Rotate the Box and Drop the Stones
Updated
Problem statement
You have a box seen from the front. It is a grid with m rows and n columns, given as an array of strings box. box[i] is row i, counted from the top, and each character is one cell:
'O'is a stone.'X'is a fixed block. It never moves.'.'is an empty cell.
Turn the box 90 degrees clockwise, so it now stands on what used to be its right side. The old top row becomes the new rightmost column, and the old rightmost column becomes the new bottom row.
Then gravity pulls every stone straight down. A stone keeps falling until the cell right below it is the bottom of the box, a fixed block, or a stone that has already stopped. Stones never move sideways, and blocks stay where the turn put them.
Return the box after the turn, once every stone has stopped. Give it as n strings of length m, from the new top row to the new bottom row, using the same three characters.
Function
rotateAndDrop(box: String[]) → String[]Examples
Example 1
box = ["O.XO.","OO..X"]return = ["..",".O","OX","O.","XO"]After the turn, the box has 5 rows and 2 columns. The old bottom row "OO..X" becomes the left column. Read from top to bottom, it is O, O, ., ., X. The old top row "O.XO." becomes the right column: O, ., X, O, ..
- Left column: both stones fall until they sit on the block at the bottom. The column becomes
., ., O, O, X. - Right column: the top stone falls one cell and stops on the block. The stone under the block falls to the bottom. The column becomes
., O, X, ., O.
Reading the two columns row by row gives ["..", ".O", "OX", "O.", "XO"].
Example 2
box = ["O.O."]return = [".",".","O","O"]The box has one row, so after the turn it has one column of 4 cells. From top to bottom it reads O, ., O, .. Both stones fall to the bottom, and the upper one stops on the lower one. The column becomes ., ., O, O.
Example 3
box = ["X.O","O.X","..."]return = ["..X",".O.",".XO"]After the turn, the left column is the old bottom row "...", so it stays empty. The middle column is the old middle row "O.X": O, ., X from top to bottom. Its stone falls one cell and stops on the block. The right column is the old top row "X.O": X, ., O. The block stays at the top even with empty space under it, and the stone is already at the bottom.
Constraints
1 <= m == box.length <= 1001 <= n == box[i].length <= 100- All rows of
boxhave the same lengthn. - Each character of
box[i]is'O','X', or'.'.
More ZipRecruiter Online Assessment questions
- Four-Level Banking System with Scheduled PaymentsOA · Seen Oct 2026
- Count Three-Digit Numbers with Distinct DigitsOA · Seen Oct 2024
- Longest Frequency-Bounded SubarrayOA · Seen Oct 2024
- Minimum Changes to Draw a YOA · Seen Oct 2024
- Apply Binary State OperationsOA · Seen Sep 2024
- Simulate Orchard Rot SpreadOA · Seen Sep 2024
- Compare Odd- and Even-Index SumsOA · Seen Sep 2024
- Coordinate Removals with Column GravityOA · Seen Sep 2024
- Lexicographically Smallest End ReversalOA · Seen Sep 2024
- Recursively Expanded Shell Command CountsOA · Seen Sep 2024
Similar questions at other companies
- Maximum Diamond SumHudson River Trading · OAHudson River Trading interview questions
- Robot Navigation Around LasersRoblox · OARoblox interview questions
- Apply a Reversi MoveAmazon · OA
- Flip, Invert, and Smooth a Binary ImageAbridge · OAAbridge interview questions
- First Completed Mingo LineEpic · OAEpic interview questions
- Rotate a Square Matrix ClockwiseVisa · OAVisa interview questions
- Sort Concentric Matrix Borders ClockwiseByteDance · OAByteDance interview questions
- Sparse Valid Conv2D From ScratchNVIDIA · OANVIDIA interview questions
- Apply Matrix OperationsCapital One · OACapital One interview questions
- Match OutcomesAdobe · OAAdobe interview questions