← Questions

Red and black tile slides and jumps

0
GoogleCoding

A coding interview question asked at Google. It tests bfs, simulation and state space search.

A strip of cells holds red tiles (R), black tiles (B) and empty cells (_). The board is a string such as "RR_B_B".

Movement rules

  • A red tile only moves right. A black tile only moves left.
  • Slide: a tile moves one cell in its direction into an empty cell.
  • Jump: a tile moves two cells in its direction, over exactly one tile of the other color, into an empty cell. The jumped tile is not removed. A tile can never jump a tile of its own color.
  • One tile moves per move. Tiles never leave the board.

Part 1. Implement valid_moves(board: str) -> list[tuple[int, int]]. Return every legal move as (from_index, to_index), ordered by from_index (a tile with both a slide and a jump lists the slide first).

Part 2. Implement min_moves(start: str, target: str) -> int. Return the fewest moves that turn start into target, or -1 if it cannot be done. Be ready to return the move sequence too.

Constraints: 0 <= len(board) <= 20; both boards have the same length and use only R, B and _.

Example 1

valid_moves("R_B")  -> [(0, 1), (2, 1)]
valid_moves("RB_")  -> [(0, 2)]      # red jumps black; black is blocked
valid_moves("RR_")  -> [(1, 2)]      # a red cannot jump a red

Example 2

min_moves("R_B", "B_R") -> 3
  R_B -> _RB   (red slides 0 -> 1)
  _RB -> BR_   (black jumps 2 -> 0)
  BR_ -> B_R   (red slides 1 -> 2)

min_moves("RB", "BR") -> -1   # no tile can move at all

Clarifying questions

  • In part 2, do both boards need the same number of red and black tiles? (If not, answer -1 at once.)
  • Should valid_moves on an empty board return an empty list?
  • Can a jump land on a cell two away when the jumped cell is empty? (No: it must hold exactly one tile of the other colour.)

What a strong answer covers

  • Part 1: scans the board once; for each red, checks the slide to i + 1 and the jump over a black at i + 1 to i + 2; mirrored for blacks; lists slides before jumps for the same tile.
  • Part 2: breadth-first search over board strings from start, generating moves with part 1, with a visited set; returns the depth at which target appears.
  • Bounds the search: reds only move right and blacks only left, so every move changes positions in one direction, the number of moves is bounded, and the reachable states are far fewer than 3^20.
  • Keeps parent pointers to return the move sequence, and checks tile counts and quick impossibility (a red that would have to move left) before searching.
  • Tests the examples, a board with no moves, a board equal to the target (0), and boards needing several jumps.

Follow-ups

  • Return the move sequence as well as its length.
  • Allow jumping over a tile of the same colour, or over several tiles.
  • Make the search faster with bidirectional BFS or A*.

Where it's asked

What it tests

bfs · simulation · state space search · invariants

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding