FastPrepApply a 2048 Board Move

Apply a 2048 Board Move

Glean logoGlean● MediumFULLTIMEPHONE SCREEN
Learn

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 every row or column, remove zeros toward the chosen edge, merge equal adjacent tiles once per move from that edge outward, and compact again. 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]]

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]]

The two columns are processed independently toward the top edge.

Constraints

  • board.length == 4 and every row has length 4.
  • Every cell is zero or a positive power of two no greater than 1073741824.
  • direction is one of LEFT, RIGHT, UP, or DOWN.
  • Every merged value fits signed 32-bit arithmetic.

More Glean problems

See Glean hiring insights
public int[][] move2048(int[][] board, String direction) {
    // Write your code here.
}
board[[2,0,2,2],[4,4,4,4],[0,0,2,2],[2,2,2,0]]
direction"LEFT"
expected[[4,2,0,0],[8,8,0,0],[4,0,0,0],[4,2,0,0]]
Checking account…