Implement 2048 Game Logic
Company: Glean
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Implement the core logic for the 2048 game.
You are given an `n x n` integer board where `0` represents an empty cell and every nonzero value represents a tile value. Implement a method such as `move(direction)` that updates the board after one move in one of four directions: `up`, `down`, `left`, or `right`.
Rules:
- All tiles slide as far as possible in the chosen direction.
- Adjacent tiles with the same value merge into one tile with double the value.
- A tile may participate in at most one merge per move.
- Empty cells are filled with `0` after sliding and merging.
- You do not need to implement the UI. If random new-tile generation is needed, keep it separate from the deterministic movement logic.
Examples for a left move on a single row:
- `[2, 0, 2, 0] -> [4, 0, 0, 0]`
- `[2, 2, 2, 0] -> [4, 2, 0, 0]`
- `[2, 2, 2, 2] -> [4, 4, 0, 0]`
Follow-up: implement `isGameOver()`, which returns `true` if no legal move remains. A game is over when the board has no empty cells and there are no horizontally or vertically adjacent equal tiles.
Quick Answer: This question evaluates array and matrix manipulation, stateful simulation of game mechanics, and handling edge cases such as merge constraints, single-merge-per-move rules, and empty-cell management.