Jane StreetCoding·60 minFree preview
Connect-N on an Unbounded Board
Implement Connect-N on a board unbounded both horizontally (any integer column, including negatives) and vertically: insert(col, player) places a piece and reports whether a player now has N consecutive pieces along a row or a column. The hard part is the row check across columns of uneven height; the key hint is to inspect only the rows and column touched by the latest insert. A push-from-bottom variant shifts the whole column up on each insert, so several rows change at once and both players can win on the same move.
SWE
RS
simulation
grid
hashmap
data-structure
medium
Frequency
Low
Last asked
2026-03-22
Stage
phone-screen
Problem
Implement a Connect-N style two-player game on a board with no fixed bounds:
- Columns extend infinitely in both horizontal directions (column ids
…, -2, -1, 0, 1, 2, …). - Each column is unbounded vertically; pieces obey gravity, so a column is a contiguous stack.
- Two players,
'R'(Red) and'B'(Blue), alternate turns. A dropped piece falls to the lowest free cell of its column. - Implement
insert(col, player)(also calledmove) and determine whether a player now hasN(also writtenk) consecutive pieces.kis fixed when the game is initialized. - "Consecutive" means along a row or along a column.
- First build a correct board without worrying about efficiency, then add the win check.
Staged version:
- Part 1 — only apply the move: update the board for the given player and column.
- Part 2 —
movereturns the winner's name when the move completeskin a row horizontally or vertically (otherwiseNone). If several lines complete at once, the player who just moved wins. - Part 3 — diagonal lines of
kalso count as a win.
class InfiniteConnect:
def __init__(self, k: int): ...
def move(self, player: str, col: int) -> str | None:
# returns the player's name if this move completes k in a line, else None
...Variants
Push-from-bottom insertion
- The new piece enters column
colat the bottom. If the column already has pieces, every existing piece is pushed up one cell. - Because the whole column shifts, every row that column occupies can change on a single insert.
- Both players can reach
kin a row on the same move — return every winner (a set works). - The insertion direction is described differently across candidates; clarify whether a piece lands on top of the column or enters at the bottom before coding.
Hints
Pitfalls
- Sparse occupancy is the tricky edge: neighbouring columns have different heights, so the row scan must treat a column shorter than the current row as an empty cell. Candidates have run out of time on exactly these boundary cases.
- Re-scanning the whole board after every insert is the trap; the interviewer's key hint is to check only the affected rows and column.
- Under push-from-bottom, one insert can create runs for both players at once.
- Code is not executed, so loop bounds on the row scan have to be verified by hand-tracing.
- Conflating the
boarddict with theheightsdict, or checking only one direction and returning early, are easy mistakes; trace a move at a negative column and at row 0.
Talking Points
- Sparse dict keyed by
(col, row)— negative indices and unbounded growth both break array-based storage. - Separate column heights from the board — the drop becomes
O(1)instead of a column scan. - Win-check locality — only lines through the new piece can complete, which keeps a move at
O(k)instead ofO(n). - Loop over direction vectors instead of copy-pasting four while loops — less error-prone, and Part 3 becomes a two-line change.
- Simultaneous wins — the mover is the only player who can form a new line, so returning on the first hit is enough.
Follow-ups
Preparation
- Implement both insertion rules on
defaultdict(list)columns with negative column ids, then hand-trace a horizontal win spanning columns of different heights. - Write a win check that returns a set of winners and trace a push-from-bottom move that completes runs for both players.
- Extend the direction-vector loop from horizontal/vertical to include both diagonals and hand-trace a diagonal win.
- Do a timed 60-minute dry run without executing the code: write it, then test it purely by reading.
Was this article helpful?
Comments
Sign in to join the discussion
Loading...
