Implement Connect Four game
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates implementation skills for game state management, move handling, and efficient winner-detection algorithms, testing competence in data structures, algorithmic complexity analysis, and correctness under edge cases.
Constraints
- The board size is fixed at 6 rows by 7 columns.
- 0 <= len(moves) <= 100
- A valid column index is an integer from 0 to 6.
Examples
Input: ([],)
Expected Output: 'Pending'
Explanation: No moves have been played, so nobody has won and the board is not full.
Input: ([3, 3, 2, 2, 1, 1, 0],)
Expected Output: 'R'
Explanation: Red forms a horizontal line on the bottom row across columns 0, 1, 2, and 3.
Input: ([0, 1, 0, 1, 2, 1, 3, 1],)
Expected Output: 'Y'
Explanation: Yellow stacks 4 pieces vertically in column 1.
Input: ([0, 1, 1, 2, 4, 2, 2, 3, 4, 3, 5, 3, 3],)
Expected Output: 'R'
Explanation: Red creates a diagonal from bottom-left to top-right.
Input: ([0, 0, 0, 0, 0, 0, 0],)
Expected Output: 'Invalid'
Explanation: A Connect Four column holds only 6 pieces. The 7th move in the same column is invalid.
Hints
- Keep track of the next open row in each column so you can place a piece in O(1) time.
- After placing a piece, only check lines that pass through that new piece: horizontal, vertical, and the two diagonals.