FastPrepCount Pieces After Othello Moves
Problems/Count Pieces After Othello Moves

Count Pieces After Othello Moves

Hudson River Trading logoHudson River Trading● EasyINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report comes from a Hudson River Trading intern online assessment. Its second task was an Othello-style board. Pieces are placed one move at a time, each new piece turns over the other color's pieces trapped between it and the mover's own pieces, the same color may move twice in a row, and the answer is the final number of black and white pieces.

The report does not say how the starting board is given, which directions count, what happens when a move turns nothing over, or how big the input can be. Here the board comes as rows of B, W and dots, all 8 directions count as in Othello, a move that flips nothing is still placed, moves never land on a taken cell, X is the column and Y is the row, and you return the two counts as an array.

Our version is about 80% the same as the reported question.

Black and White place pieces on an n x n board, as in the game Othello (also called Reversi). You are given the starting board and a list of moves. Apply the moves in order, then return how many black pieces and how many white pieces are on the board.

board is a list of n strings, each of length n. board[y][x] is the cell in row y and column x. 'B' is a black piece, 'W' is a white piece, and '.' is an empty cell. Column 0 is on the left and row 0 is at the top, so x grows to the right and y grows toward the bottom.

Each move is a string "P X Y". P is the color of the new piece, B or W. X is its column and Y is its row. The players do not have to take turns, so the same color can move several times in a row.

When a piece is placed, check each of the 8 directions from it: left, right, up, down, and the 4 diagonals.

  • Start at the cell next to the new piece and walk outward while the cells hold pieces of the other color.
  • If at least one such piece was passed and the walk stops on a piece of the mover's color, every piece passed flips to the mover's color.
  • If the walk stops on an empty cell or at the edge of the board, nothing flips in that direction.
  • Flipped pieces do not cause any more flips.
  • A move is placed even when it flips nothing.

Every move goes on a cell that is empty at that moment.

Return an array [black, white] with the number of black pieces and the number of white pieces after all the moves.

Function

countPieces(board: String[], moves: String[]) → int[]

Examples

Example 1

board = ["....",".WB.",".BW.","...."]moves = ["B 3 2","B 0 0"]return = [6,0]
board
01230....1.WB.2.BW.3....

The board starts with 2 black and 2 white pieces.

  • "B 3 2" puts a black piece in column 3, row 2. To its left are the white piece in column 2 and then a black piece in column 1, so that white piece flips. No other direction has a white piece next to the new piece. Now there are 4 black and 1 white.
  • "B 0 0" is Black again, because the players do not have to take turns. The piece goes in the top-left corner. Down and to the right are the white piece in column 1, row 1 and then the black piece in column 2, row 2, which the first move flipped. So that white piece flips too. Now there are 6 black and 0 white.

Example 2

board = ["B.B.B",".WWW.","BW.WB",".WWW.","B.B.B"]moves = ["B 2 2","W 1 0"]return = [17,1]
board
012340B.B.B1.WWW.2BW.WB3.WWW.4B.B.B

The board starts with 8 black and 8 white pieces.

  • "B 2 2" puts a black piece in the center. In each of the 8 directions there is one white piece next to it and then a black piece, so all 8 white pieces flip. Now there are 17 black and 0 white.
  • "W 1 0" puts a white piece in column 1 of the top row. Every line from it reaches an empty cell or the edge of the board without meeting another white piece, so nothing flips. The piece is still placed, so the answer is 17 black and 1 white.

Example 3

board = [".BW.","....","BW.W","B.W."]moves = ["W 0 0","B 2 2","B 3 3"]return = [5,5]
board
01230.BW.1....2BW.W3B.W.

The board starts with 3 black and 4 white pieces.

  • "W 0 0": to the right are a black piece and then a white piece, so the black piece in column 1, row 0 flips. Now there are 2 black and 6 white.
  • "B 2 2": to the left are a white piece and then a black piece, so the white piece in column 1, row 2 flips. To the right, the white piece in column 3 is followed by the edge of the board. Below, the white piece in row 3 is also followed by the edge. Neither of those flips. Now there are 4 black and 5 white.
  • "B 3 3": to the left, the white piece in column 2 is followed by an empty cell. Above, the white piece in row 2 is also followed by an empty cell. So nothing flips, and the answer is 5 black and 5 white.

Constraints

  • 1 <= n <= 50, where n == board.length
  • board[i].length == n
  • Each board[i][j] is 'B', 'W', or '.'.
  • 1 <= moves.length <= 2500
  • Each moves[i] has the form "P X Y" with single spaces, where P is B or W, and X and Y are whole numbers from 0 to n - 1 written without leading zeros.
  • Each move's cell is empty at the moment that move is made.

More Hudson River Trading Online Assessment questions

  • Count Palindrome Addition ChoicesOA · Seen Nov 2026
  • Count Multiples of Three with Two SevensOA · Seen Oct 2026
  • Recursively Expanded Shell Command CountsOA · Seen Oct 2026
  • Rotate Matrix Regions Around the DiagonalsOA · Seen Oct 2026
  • Maximum Diamond SumOA · Seen Oct 2026
  • Smallest Most-Lit Street PositionOA · Seen Oct 2026
  • Rearrange String by Alternating EndsOA · Seen Sep 2026
  • Most Items Within Budget from a Start IndexOA · Seen Sep 2026
  • Count Sawtooth SubarraysOA · Seen Aug 2026
  • Backtick Identifier ConverterOA · Seen Jun 2026

Similar questions at other companies

  • Wind-Blown Leaves Remaining in a GridAbridge · OAAbridge interview questions
  • Trapped Characters in a GridAdobe · OAAdobe interview questions
  • Tic-Tac-Toe Board StatusPinecone · OA
  • Robot Navigation Around LasersRoblox · OARoblox interview questions
  • Apply a Reversi MoveAmazon · OA
  • First Completed Mingo LineEpic · OAEpic interview questions
  • Rotate a Square Matrix ClockwiseVisa · OAVisa interview questions
  • Sort Concentric Matrix Borders ClockwiseByteDance · OAByteDance interview questions
  • Sparse Valid Conv2D From ScratchNVIDIA · OANVIDIA interview questions
  • Apply Matrix OperationsCapital One · OACapital One interview questions

Hudson River Trading interview experiences

  • Hudson River Trading Quant Video Interview: 1 Experience
  • HRT SWE Intern and Onsites: 34 Experiences (Part 2 of 2)
  • HRT Python Track Interviews: 64 Experiences (Part 1 of 2)
  • HRT New Grad Python Track Interviews: 9 Experiences
All Hudson River Trading interview questionsSee Hudson River Trading hiring insights
Saved
class Solution {
    public int[] countPieces(String[] board, String[] moves) {
        // Write your code here.
    }
}
Checking account…