cswork / libraryOpen practice workspace

Coding questions · Glean

Implement 2048 tilt move

This question evaluates a candidate's ability to implement array and matrix manipulation, deterministic state-transition logic, and rule-based merging behavior in a grid-based simulation.

Problem: Implement a 2048 “tilt” (move) operation

You are given a 4×4 board for the game 2048. Each cell contains either 0 (empty) or a power of two (2, 4, 8, ...). When the player swipes in one of four directions (up, down, left, right), the board updates as follows:

  1. Shift: All non-zero tiles move as far as possible in the swipe direction.
  2. Merge: If two tiles with the same value collide during the move, they merge into one tile with value equal to the sum (i.e., doubled).
  3. Single-merge rule: A tile can be merged at most once per move.
  4. After merges, tiles continue shifting to fill gaps in the swipe direction.

Task

Implement a function that applies exactly one swipe (tilt) to the board.

Function signature (language-agnostic)

  • Input:
    • board: 4×4 integer matrix
    • dir: one of {up, down, left, right}
  • Output:
    • The updated 4×4 board after the move

Examples

  • Swipe left:
    • [2, 0, 2, 4] → [4, 4, 0, 0]
  • Swipe left (single-merge rule):
    • [2, 2, 2, 0] → [4, 2, 0, 0] (not [8, 0, 0, 0])
  • Swipe left:
    • [4, 4, 4, 4] → [8, 8, 0, 0]

Constraints / Notes

  • Board size is always 4×4.
  • Only implement the tilt/move logic (no random tile spawning, no scoring required unless you choose to return it as an extra).
  • Be careful with edge cases around multiple merges in a line and ensuring each tile merges at most once per move.

Implement 2048 Tilt Move

You are given a 4x4 board for the game 2048. Each cell contains either 0 (empty) or a power of two (2, 4, 8, ...). When the player swipes in one of four directions ('up', 'down', 'left', 'right'), the board updates as follows:

  1. Shift: All non-zero tiles move as far as possible in the swipe direction.
  2. Merge: If two tiles with the same value collide during the move, they merge into one tile whose value is their sum (i.e. doubled).
  3. Single-merge rule: A tile can be merged at most once per move.
  4. After merges, tiles continue shifting to fill gaps in the swipe direction.

Implement a function tilt(board, dir) that applies exactly one swipe to the board and returns the updated 4x4 board. dir is one of the strings 'up', 'down', 'left', 'right'.

Examples (single row, swipe left):

  • [2, 0, 2, 4] -> [4, 4, 0, 0]
  • [2, 2, 2, 0] -> [4, 2, 0, 0] (single-merge rule, NOT [8, 0, 0, 0])
  • [4, 4, 4, 4] -> [8, 8, 0, 0]

Merges resolve from the swipe-edge inward: when scanning a line in the direction of travel, the first eligible same-value pair merges, and a tile that has already merged cannot merge again in the same move.

Hints

  1. Reduce the 2D problem to a 1D one: solve 'slide and merge a single line toward index 0', then apply it to the correct row/column orientation per direction.
  2. For a single line: first drop the zeros to pack non-zero tiles together, then scan left-to-right merging equal adjacent pairs, advancing past a merged pair so the same tile is never merged twice.
  3. Handle 'right' by reversing each row, applying the left-slide, then reversing back. Handle 'up'/'down' by transposing to columns (and reversing for 'down').