← Questions

Can one board be turned into another by moving pieces?

0
GoogleCodingLast asked Apr 2026

A coding interview question asked in Google's first round (coding), last asked Apr 2026. It tests two pointers and strings.

A board is a string of L, R and _ (empty). An L piece can move only left and an R piece only right, one empty cell at a time, and no piece can jump over another. Decide whether the start board can be turned into the target board.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • can_transform(start: str, target: str) -> bool — both strings have the same length.

Example

can_transform("_L__R__R_", "L______RR")  ->  True     # L moves left 1, the first R right 3, the second R right 1
can_transform("R_L_", "__LR")            ->  False    # the R would have to pass the L
can_transform("_R", "R_")                ->  False    # R cannot move left

Follow-up: walls

Add a wall | that never moves and that no piece can cross. Both boards must have their walls in the same cells.

can_transform("R_|_L", "_R|L_")  ->  True
can_transform("R_|__", "__|_R")  ->  False    # the R cannot cross the wall

Constraints

  • 1 <= len(start) <= 10^5.

Clarifying questions

  • Can a piece move more than one cell per move? (It does not matter: any number of single steps is allowed.)
  • Are the boards the same length? Must the piece counts match?
  • In the follow-up, can a wall's position differ between the boards?

What a strong answer covers

  • The invariant: pieces never pass each other, so the sequence of pieces with blanks removed must be the same in both boards.
  • Matching the i-th piece of each board with two pointers: an L may only end at the same or a smaller index, an R at the same or a larger index. O(n) time, O(1) extra space.
  • Why these conditions are also sufficient (each piece can be moved into place in some order).
  • The wall follow-up: walls split the board into independent segments; check walls line up, then apply the same test per segment, or treat | as a piece that must stay at the same index.
  • Tests: no pieces, identical boards, pieces in the wrong order, a piece that needs to move the wrong way.

Follow-ups

  • Return the minimum number of single-cell moves (the sum of distances between matched pieces).
  • Pieces can move at most k cells in total.
  • A third piece type that can move both ways.

Where it's asked

  • Google · First round: coding · Last asked Apr 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI

What it tests

two pointers · strings

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