Red and Black Tile Moves
Updated
Problem statement
Given a one-dimensional board start and a target board end of the same length, return the list of boards that moves the tiles from start to end one move at a time. Return an empty list if end cannot be reached.
Each cell is "R" (a red tile), "B" (a black tile) or "_" (empty). A move changes the position of one tile:
- Red tiles only move right, and black tiles only move left.
- A tile can step into the next cell in its direction if that cell is empty.
- A tile can also jump over exactly one tile of the other color, if the cell just past it is empty.
The list starts with start, ends with end, and each board comes from the board before it by one move. If start equals end, return [start].
When more than one list works, return the one that comes first: find the first board where two lists differ, and compare those two boards as strings by character code, so "B" < "R" < "_".
Note: every list that works has the same number of moves, so this rule always picks exactly one answer.
Function
validMoves(start: String[], end: String[]) → List<List<String>>Examples
Example 1
start = ["R","_","B","B"]end = ["B","_","B","R"]return = [["R","_","B","B"],["_","R","B","B"],["B","R","_","B"],["B","R","B","_"],["B","_","B","R"]]The red tile steps right, a black tile jumps left over it, the other black tile steps left, and then the red tile jumps right over that black tile. Each board differs from the one before it by one move.
Example 2
start = ["R","R","_","_"]end = ["_","_","R","R"]return = [["R","R","_","_"],["R","_","R","_"],["R","_","_","R"],["_","R","_","R"],["_","_","R","R"]]Two lists of four moves work. They first differ at the third board, where "R__R" comes before "_RR_" because "R" comes before "_". So this list is returned.
Example 3
start = ["R","B","_"]end = ["B","R","_"]return = []The two tiles must pass each other, and passing takes a jump. The black tile has no cell to jump into. The red tile can jump, but it lands in cell 2, past cell 1 where it must end, and it can never move left. So the answer is empty.
Example 4
start = ["_","B","B"]end = ["B","B","_"]return = [["_","B","B"],["B","_","B"],["B","B","_"]]The first black tile steps left into the empty cell, and then the second black tile steps left into the cell it left.
Example 5
start = ["R","R","B"]end = ["B","R","_"]return = []The boards hold different tiles. start has two red tiles and one black tile, while end has one of each. Moves never add or remove tiles, so the answer is empty.
Example 6
start = ["_","R","_"]end = ["B","R","_"]return = []end has a black tile that start does not have, so the answer is empty.
Example 7
start = ["B","_","R"]end = ["R","_","B"]return = []The black tile can only move left and the red tile can only move right, so they can never swap sides.
Example 8
start = ["_","R","R","B"]end = ["B","R","R","_"]return = []The black tile would have to jump over both red tiles, but a jump passes over exactly one tile. The black tile has no empty cell to move into, and each red tile is blocked by the tile on its right.
Example 9
start = ["R","_","_","B"]end = ["R","B","_","_"]return = [["R","_","_","B"],["R","_","B","_"],["R","B","_","_"]]The black tile steps left twice. The red tile does not move.
Constraints
1 <= start.length <= 12end.length == start.length- Each
start[i]andend[i]is"R","B"or"_".
More WhatNot Phone Screen questions
- Merge Sorted Squares of Two ArraysPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adjacent Pair and Run RemovalPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey Paths from a Log StreamPHONE SCREEN · Seen Nov 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Find a Word Path Moving Right or DownIntuit · OA
- Word SearchBloomberg LP · OA · PHONE SCREEN · ONSITE INTERVIEW
- Count Complete Tic-Tac-Toe GamesDecagon · PHONE SCREEN · ONSITE INTERVIEW
- House Robber IIIOkta · PHONE SCREEN
- All Halfway Courses Across Course TracksCrunchyroll · PHONE SCREEN
- House Robber IIIGoldman Sachs · PHONE SCREEN
- Binary Tree Target-Sum PathsByteDance · PHONE SCREEN
- House Robber IIIVisa · PHONE SCREEN
- Count Starts Reaching a Grid MaximumDuolingo · PHONE SCREEN
- Minimum Root-to-Leaf Cut CostGoogle · PHONE SCREEN · ONSITE INTERVIEW