Airbnb Phone Screen : Connect Four
Anonymous User
2511

Connect Four is a two-player connection board game.
The players choose a color and then take turns dropping colored tokens into a seven-column, six-row vertically suspended grid.
The pieces fall straight down, occupying the lowest available space within the column.
The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one's own tokens. - Wikipedia

The input is a list of columns played successively by the two players.
The output is to display the Connect Four grid associated when the game finishes.

If a winning state is reached or if a wrong move is played, the game should stop even if there are still columns in the list.
In that case, only the tokens that have been played should be displayed in the grid.

The goal is to write code that is well-structured, rather than a solution that is too complex.
Optimisation may be discussed during follow-up questions.

Examples:

Input (column full):
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]

Output:
| | |X| | | | |
| | |0| | | | |
| | |X| | | | |
| | |0| | | | |
| | |X| | | | |
| | |0| | | | |

In this example, we start playing with the 0 token.
The 7th item of the input is invalid because the column is already full.

Comments (6)