← Databricks Interview Insights
My first instinct was to just scan the whole board after every move, which works but felt lazy.
Clarify requirements and constraints, then design a class that maintains board state and efficiently checks for a win after each move. Implement the win check by tracking counts of marks per row, column, and diagonal for each player, updating them on each move and checking if any reaches n.
Pro tip: Mention that you can optimize the win check to O(1) per move by maintaining row, column, and diagonal counts for each player, and that this approach scales well for large n. Also, discuss handling invalid moves and edge cases like a full board with no winner.
Ask about board size, player representation, move validation, and what to return if the game ends in a draw. Confirm that after each move, you need to return the winning player or 0.
Define a class with a constructor that initializes an n x n board and data structures to track counts. Include a move method that takes row, column, and player, validates the move, updates the board and counts, and returns the winner.
Maintain arrays for each player: row counts, column counts, and two diagonal counts. After a move, increment the relevant counts and check if any equals n. This gives O(1) time per move.
Check for invalid moves (out of bounds, occupied cell, wrong player turn) and throw exceptions or return an error. Also, track the number of moves to detect a draw when the board is full.
Explain that the solution uses O(n^2) space for the board and O(n) additional space for counts, with O(1) time per move. Walk through a few test cases to verify correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.