KatInterview

Sliding Tile Board: Merge Logic for All Four Directions

Arrays & HashingModel answer
MediumModel answerArrays & Hashingarrayssimulationmatrix

Last updated

Implement the slide-and-merge move for a 2048-style grid in all four directions, sharing a single merge routine across every direction.

Sliding Tile Board: Merge Logic for All Four Directions

You are building the core move engine for a sliding-tile puzzle played on an n × n grid of integers. Each cell holds either a positive integer (a tile) or 0 (an empty cell). A player move chooses a direction and the board responds by sliding every tile as far as possible toward that side, then merging tiles that collide.

Move Rules

  • Tiles slide until they hit a wall or another tile; all gaps close.
  • Two adjacent tiles with equal values merge into one tile whose value is their sum, placed at the leading position.
  • A tile produced by a merge is frozen for the rest of that move — it cannot merge again.
  • When three or more equal tiles are lined up, the pair closest to the wall the tiles are sliding toward merges first.
Left move examples (one row shown):

[2, 2, 2, 0]  →  [4, 2, 0, 0]   (first pair merges; third tile slides)
[2, 0, 2, 4]  →  [4, 4, 0, 0]   (gap closes, equal pair merges)
[4, 4, 4, 4]  →  [8, 8, 0, 0]   (two independent merges)
[2, 4, 8, 16] →  [2, 4, 8, 16]  (nothing to merge or slide)

The same rules apply to right, up, and down — only the direction toward which tiles collapse changes.

Clarifying Questions to Consider

Before coding, think through these points (you do not need to ask them, but your solution should reflect the answers):

  • The board is always square; n can be any positive integer.
  • A move should return a new board and a boolean indicating whether anything changed. Do not modify the input in place.
  • Spawning a new random tile after a move and tracking the score are out of scope for this exercise.

Part 1 — Move Left

Implement move_left(board). It must:

  • Accept a 2-D list of integers and return (new_board, changed).
  • Apply the slide-and-merge rules to every row independently.
  • Pass a test suite you write and run before continuing to Part 2.

Your tests should cover at minimum:

  • A row with a gap between equal tiles
  • Three equal tiles in a row
  • Four equal tiles in a row
  • A fully packed row with no merges possible
  • A row that is already solved (all tiles at the left, no adjacent duplicates)
  • An empty row

Part 2 — All Four Directions

Extend your solution to move(board, direction) where direction is one of "left", "right", "up", or "down".

Constraint: the merge routine from Part 1 must be reused as-is. Do not write a separate merge function for each direction.

Two design approaches are worth knowing:

  • Rotation: transform the board so every move becomes a left move, then transform back.
  • Index remapping: leave the board in place and iterate rows or columns in the order the chosen direction dictates.

Implement one approach. Be ready to explain the trade-offs between the two: which is easier to read, which is more likely to produce an off-by-one indexing error, and what the computational cost difference is.

Add tests for right, up, and down. For each direction, include at least one case where a merge happens and one where nothing changes.

Hints

Hint 1

Start by writing a function that processes a single list (one row or one column), not the whole board. Once that is correct, applying it to the full grid is straightforward.

Hint 2

For the single-line merge: strip the zeros out first, then make a single left-to-right pass merging adjacent equal values, then pad with zeros at the right. This keeps the logic simple and avoids fiddly index arithmetic.

Hint 3

For multi-direction support, the rotation approach lets you reuse move_left directly: rotating the board 90° clockwise turns a 'up' move into a 'left' move. Map each direction to a number of clockwise rotations, apply them, call move_left, then rotate back the same amount in the opposite direction.