Apply a 2048 Board Move
Updated
Problem statement
You are given a fixed 4 x 4 2048 board and one direction: "LEFT", "RIGHT", "UP", or "DOWN". Apply one move and return the resulting board.
For each row or column along the chosen direction:
- Slide its nonzero tiles toward the chosen edge, closing the gaps.
- Starting at that edge and moving away from it, merge adjacent equal tiles once per move.
- Slide the tiles toward that edge again to close any new gaps.
A tile created by a merge cannot merge again during the same move. Do not spawn a random tile.
Function
move2048(board: int[][], direction: String) → int[][]Examples
Example 1
board = [[2,0,2,2],[4,4,4,4],[0,0,2,2],[2,2,2,0]]direction = "LEFT"return = [[4,2,0,0],[8,8,0,0],[4,0,0,0],[4,2,0,0]]Moved or new
Each row compacts left and each original tile participates in at most one merge.
Example 2
board = [[2,0,0,2],[2,0,0,2],[4,0,0,4],[0,0,0,0]]direction = "UP"return = [[4,0,0,4],[4,0,0,4],[0,0,0,0],[0,0,0,0]]Moved or new
The two columns are processed independently toward the top edge.
Constraints
board.length == 4and every row has length4.- Every cell is zero or a positive power of two no greater than
1073741824. directionis one ofLEFT,RIGHT,UP, orDOWN.- Every merged value fits signed 32-bit arithmetic.
More Glean Phone Screen questions
Similar questions at other companies
- Spiral Order with ObstaclesTemu · PHONE SCREEN
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEW
- Spiral Matrix TraversalByteDance · PHONE SCREEN
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN
- Longest Consecutive Same-Color LineGoogle · PHONE SCREEN
- Process Minesweeper Game OperationsAmperity · PHONE SCREEN
- Diagonal TraverseHive · PHONE SCREEN
- Game of Life on a Finite and an Infinite BoardCitadel · PHONE SCREEN
- Build a Bird’s-Eye-View Height GridNVIDIA · PHONE SCREEN
- Multi-Channel Valid ConvolutionTesla · PHONE SCREEN