Implement 2048 tilt move
Problem: Implement a 2048 "tilt" (move) operation
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 according to the following rules:
- Shift: All non-zero tiles move as far as possible in the swipe direction.
- Merge: If two tiles with the same value collide during the move, they merge into one tile with a value equal to their sum (i.e., doubled).
- Single-merge rule: A tile can be merged at most once per move.
- After merges, tiles continue shifting to fill any newly created gaps in the swipe direction.
Task
Implement a function that applies exactly one swipe (tilt) to the board.
Function Signature (language-agnostic)
- Input:
board: A 4x4 integer matrix representing the game board.dir: A string indicating the swipe direction, one of{up, down, left, right}.
- Output:
- The updated 4x4 board after the move.
Examples
- Swipe left:
[2, 0, 2, 4]→[4, 4, 0, 0]
- Swipe left (demonstrating single-merge rule):
[2, 2, 2, 0]→[4, 2, 0, 0](Note: not[8, 0, 0, 0])
- Swipe left:
[4, 4, 4, 4]→[8, 8, 0, 0]
Constraints / Notes
- The board size is always 4x4.
- Only implement the tilt/move logic. Do not implement random tile spawning or scoring.
- Pay close attention to edge cases involving multiple merges in a line and ensuring each tile merges at most once per move.
AI interview prep
Core dilemma
The central engineering conflict is correctly and efficiently managing the sequential, dependent state changes (shift, merge, then shift again) for all four directions, while strictly adhering to the "single-merge per tile per move" rule.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement 2048 tilt move
Problem: Implement a 2048 "tilt" (move) operation
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 according to the following rules:
- Shift: All non-zero tiles move as far as possible in the swipe direction.
- Merge: If two tiles with the same value collide during the move, they merge into one tile with a value equal to their sum (i.e., doubled).
- Single-merge rule: A tile can be merged at most once per move.
- After merges, tiles continue shifting to fill any newly created gaps in the swipe direction.
Task
Implement a function that applies exactly one swipe (tilt) to the board.
Function Signature (language-agnostic)
- Input:
board: A 4x4 integer matrix representing the game board.dir: A string indicating the swipe direction, one of{up, down, left, right}.
- Output:
- The updated 4x4 board after the move.
Examples
- Swipe left:
[2, 0, 2, 4]→[4, 4, 0, 0]
- Swipe left (demonstrating single-merge rule):
[2, 2, 2, 0]→[4, 2, 0, 0](Note: not[8, 0, 0, 0])
- Swipe left:
[4, 4, 4, 4]→[8, 8, 0, 0]
Constraints / Notes
- The board size is always 4x4.
- Only implement the tilt/move logic. Do not implement random tile spawning or scoring.
- Pay close attention to edge cases involving multiple merges in a line and ensuring each tile merges at most once per move.