Design Connect-N winner detector
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates data structure design and algorithmic optimization for grid-based game state management, including spatial reasoning required to detect n-in-a-row after a move.
Constraints
- 1 <= rows, cols <= 2000
- 1 <= k <= max(rows, cols)
- 1 <= len(moves) <= rows * cols
- 0 <= column < cols
- Players are positive integers (more than two players allowed)
- All moves are valid; no column is overfilled
Hints
- Track the height of each column to compute the landing row in O(1).
- Vertical check can be O(1) by maintaining, per column, the current top player's consecutive count.
- For horizontal and both diagonals, treat each line as a 1D set and maintain merged intervals of occupied indices for each (player, line-id).
- Use r - c as the key for main diagonals and r + c for anti-diagonals; use column index as the 1D coordinate.
- On each insertion, merge at most two adjacent intervals and track their lengths to detect k in O(1) average time.
Community answers
Answer by ubinexy
Answer by leni