Implement a deterministic 2048 game with spawn, move, score and game-over methods
Company: Rakuten
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a simplified, deterministic version of the 2048 sliding tile game played on a `4x4` grid.
Players issue a `move` in one of four directions: `UP`, `DOWN`, `LEFT`, or `RIGHT`. All tiles slide as far as possible in that direction. When two tiles of equal value collide during a slide they merge into a single tile whose value equals their sum; each tile may participate in **at most one merge per move**.
To keep behavior deterministic, new tiles are not spawned randomly. Instead, the caller places tiles explicitly using `spawn(row, col, value)`. The game tracks a running `score` equal to the sum of all merge values produced so far, and reports whether the game is over (no empty cell and no horizontally or vertically adjacent pair with equal values).
Implement a class `Game2048` whose constructor creates an empty board with a score of 0, with the following methods:
- `spawn(row: int, col: int, value: int) -> bool`: Place `value` at (`row`, `col`) if that cell is empty. Returns `True` on success, `False` if the cell is occupied or out of bounds.
- `move(direction: str) -> bool`: Slide all tiles in `direction` (one of `"UP"`, `"DOWN"`, `"LEFT"`, `"RIGHT"`). Equal adjacent tiles merge once each, with their sum added to the score. Returns `True` if the board changed.
- `get_score() -> int`: Return the cumulative score (sum of all merge values so far).
- `get_board() -> List[List[int]]`: Return the current 4x4 board as a list of lists of ints.
- `is_game_over() -> bool`: Return `True` if the board is full and no two adjacent cells share a value.
```hint Start with one row
Get a single row sliding left exactly right first. Then look for a way to make each of the other three directions look like that same case.
```
```hint Watch merged tiles
Trace `[4, 4, 8, 0]` moving left. If your code ends with a single 16, a tile created by a merge merged again in the same move.
```
### Examples
Example 1:
```text
Input:
["Game2048", "spawn", "spawn", "move", "get_board", "get_score"]
[[], [0, 0, 2], [0, 1, 2], ["LEFT"], [], []]
Output: [null, true, true, true, [[4, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], 4]
```
```python
game = Game2048() # empty 4x4 board, score = 0
game.spawn(0, 0, 2) # True, board[0][0] = 2
game.spawn(0, 1, 2) # True, board[0][1] = 2
game.move("LEFT") # True, row 0: [2, 2, 0, 0] -> [4, 0, 0, 0], score += 4
game.get_board() # [[4, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
game.get_score() # 4
```
Example 2:
```text
Input:
["Game2048", "spawn", "spawn", "spawn", "spawn", "move", "get_score", "get_board"]
[[], [0, 0, 4], [0, 1, 4], [0, 2, 2], [0, 3, 2], ["LEFT"], [], []]
Output: [null, true, true, true, true, true, 12, [[8, 4, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
```
```python
game = Game2048() # empty 4x4 board, score = 0
game.spawn(0, 0, 4) # True, board[0] = [4, _, _, _]
game.spawn(0, 1, 4) # True, board[0] = [4, 4, _, _]
game.spawn(0, 2, 2) # True, board[0] = [4, 4, 2, _]
game.spawn(0, 3, 2) # True, board[0] = [4, 4, 2, 2]
game.move("LEFT") # True, row 0: [4, 4, 2, 2] -> [8, 4, 0, 0], score += 8 + 4
game.get_score() # 12
game.get_board() # [[8, 4, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
```
### Constraints & Assumptions
- The board is a fixed `4 x 4` grid; empty cells are represented by `0`.
- `direction` is one of `"UP"`, `"DOWN"`, `"LEFT"`, `"RIGHT"`.
- Valid coordinates satisfy `0 <= row, col < 4`; `spawn` returns `False` for anything outside that range.
- `value` for `spawn` is a positive power of two (typically `2` or `4`).
- At most 10,000 operations in total.
### Clarifying Questions to Ask
- What should `move` return, and what should happen to the board, if `direction` is not one of the four values?
- Can I assume `spawn` values are valid powers of two, or should `spawn` reject other values?
- Is the board always 4 x 4, or should the size be a constructor argument?
- Should `get_board` return a copy, or is it acceptable to expose the internal grid?
### What a Strong Answer Covers
- How the board and score are stored, and how the four directions share the sliding and merging logic.
- Merge semantics: at most one merge per tile per move, and which pair merges first when three or more equal tiles line up.
- An accurate changed flag from `move`, plus handling of out-of-bounds or occupied `spawn` calls and unknown directions.
- A game-over check that considers empty cells and equal neighbors in both rows and columns.
- Time and space complexity, and test cases for tricky rows such as `[2, 2, 2, 2]` and `[4, 4, 8, 0]`.
### Follow-up Questions
- How would you add random tile spawning after each move while keeping the class easy to test?
- How would you support undoing the last move, including the score?
- What changes to support an `N x N` board and a win condition when a tile reaches 2048?
- A UI wants to animate each tile from its old cell to its new one. What would `move` need to return?
Overview: Implement a deterministic 4x4 version of the 2048 sliding tile game with methods to place tiles, slide and merge them in four directions, track the score and detect when no move is left. It tests class design, correct merge rules when tiles collide, and careful handling of edge cases such as moves that change nothing.