Cats, Rabbits, and Snails Board Game
Updated
Problem statement
You are given a list of moves for a two-player board game with animal pieces. Play the moves one by one, in order. Rows and columns are numbered from zero. Red moves first. After each valid move, the turn passes to the other player.
The board starts like this:
- Red cats at
[0,0]and[0,6], red rabbits at[1,2]and[1,4], and the red snail at[0,3]. - Blue cats at
[6,0]and[6,6], blue rabbits at[5,2]and[5,4], and the blue snail at[6,3].
Each move is a string "fromRow fromColumn toRow toColumn". The pieces follow these rules:
- A cat moves one to three cells in a straight line: up, down, left or right.
- A rabbit moves one to two cells diagonally.
- A snail moves exactly one cell in any of the eight directions. A snail cannot capture.
- A piece of the same color blocks a cat's or rabbit's path. If a cat or rabbit meets an enemy piece on its requested path, it captures the first one it meets and stops on that cell. This happens even if the requested destination is farther away.
- A move is invalid if it moves the wrong player's piece, breaks the movement or blocking rules, or leaves the mover's own snail threatened. A snail is threatened when an enemy cat or rabbit has a clear path to it. An invalid move changes nothing, and the same player keeps the turn.
After a valid move, look at the next player. If that player's snail is threatened and they have no valid move that makes it safe, the player who just moved wins. Add "RED_WINS" or "BLUE_WINS" for that move. Otherwise add "OK". Add "INVALID" for an invalid move. Once someone has won, add "GAME_OVER" for every move still left in the list. Return one result for each move, in the same order as the moves.
Function
playAnimalGame(moves: String[]) → String[]Examples
Example 1
moves = ["0 0 3 0","6 0 4 0","3 0 6 0","6 6 5 6"]return = ["OK","OK","OK","OK"]The red cat moves three cells along its column. The blue cat then moves to row 4. Red asks to move its cat to row 6. On the way it meets the blue cat at row 4, captures it and stops there. Blue's last move is a normal one-cell cat move. All four moves are valid, so every result is "OK".
Example 2
moves = ["1 2 2 3","5 4 6 5","2 3 4 5","5 2 3 0","6 3 5 3"]return = ["OK","OK","OK","INVALID","OK"]The first two moves are normal rabbit moves. Red's rabbit then jumps to row 4. From there it has a clear diagonal path to the blue snail, so the snail is threatened. Blue can still make the snail safe, so this move is "OK". Blue then moves its other rabbit, but the snail is still threatened. So the fourth move is "INVALID", nothing changes, and Blue keeps the turn. Blue then moves the snail to [5,3], which is safe, so the last result is "OK".
Constraints
1 <= moves.length <= 200.- Every move contains exactly four integers between
0and6. - The board always starts in the fixed arrangement described above.
- When you check threats and escapes, use the same rules as for requested moves: movement, blocking, capture and snail safety.
More Asana Onsite Interview questions
Similar questions at other companies
- Ultimate Tic-Tac-Toe Move SimulatorHebbia · PHONE SCREEN
- Randomized Collection with Duplicate ValuesOkta · ONSITE INTERVIEW
- Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEW
- Hybrid Run-Length and Bit-Packing EncoderDatabricks · ONSITE INTERVIEW
- Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEW
- Transformer Attention and SwiGLU Forward PassGoogle · ONSITE INTERVIEW
- Boggle Word FinderBloomberg LP · ONSITE INTERVIEW
- Spiral MatrixAthenahealth · ONSITE INTERVIEW
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEW
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW