Implement 2048 tilt move
Company: Glean
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
## 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.
Quick Answer: 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.