← Databricks Interview Insights
My first instinct was to scan the whole row and column after every move, which is obviously too slow.
Use an O(1) per move approach by maintaining row, column, and diagonal counters for each player. When a move is made, update the corresponding counters and check if any counter reaches n, indicating a win.
Pro tip: Clarify that the O(1) requirement applies per move, not per game, and mention that the board state can be stored implicitly via counters, reducing space complexity to O(n).
Confirm board size n, number of players (2), and that a move returns the winning player or 0. Discuss edge cases like invalid moves or moves after game ends.
Maintain arrays for row sums, column sums, and two diagonal sums for each player. Use a 2D board to track moves for validation, or use a hash set for O(1) move validation.
For a given player and position (row, col), update the corresponding row, column, and diagonal counters. Check if any counter equals n; if so, return the player.
Explain that each move takes O(1) time because only a constant number of counters are updated and checked. Space complexity is O(n) for the counters and board.
Walk through a small example (e.g., n=3) to demonstrate correctness, including a winning move and a non-winning move.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.