← Jane Street Interview Insights
The 'unbounded in both directions' part is the real constraint.
Clarify the rules (N, win condition, board bounds) and then design a data structure that tracks, for each cell, the lengths of consecutive runs in all relevant directions. For each insert, update only the affected runs in O(N) time by checking neighboring cells and combining their run lengths.
Pro tip: Mention that you would precompute or maintain run lengths for all four directions (horizontal, vertical, and both diagonals) and that the vertical direction is trivial because pieces stack, so you only need to check the new piece's row and column neighbors. Also, discuss how to handle the unbounded board efficiently using a hash map keyed by (row, col).
Ask about N (fixed or variable), whether the board is truly infinite in all directions or just columns, and if multiple players can win simultaneously. Confirm that insert must be O(N) and not O(board size).
Use a hash map to represent the sparse board, mapping (row, col) to player. Additionally, maintain for each cell the lengths of consecutive runs in the four directions (horizontal, vertical, diagonal /, diagonal \).
For a given column, find the lowest empty row (by checking the hash map or maintaining a column height map). Place the piece, then for each direction, compute the run length by combining the run lengths of the two neighboring cells in that direction (if they belong to the same player) plus 1. Update the run length for the new cell and for the endpoints of the combined runs.
After updating run lengths, if any run length >= N, return true. Otherwise, return false. Ensure that updates are O(1) per direction, so total O(N) is satisfied (since N is constant, O(1) is O(N)).
Explain that each insert touches O(1) cells (the new cell and its neighbors) and updates O(1) run lengths per direction, so O(N) time is trivially satisfied. Discuss edge cases: multiple pieces in a column, winning with more than N pieces, and memory usage for unbounded board.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.