PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Jane Street

Connect-N on an Infinite Board with Gravity

Last updated: Jul 2, 2026

Connect-N on an Infinite Board with Gravity

Company: Jane Street

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are asked to implement a variant of the classic Connect Four game, played on an unbounded board. ## Game Rules - The board consists of vertical columns indexed by integers. Columns extend **infinitely in both horizontal directions**, so any integer (including negative values) is a valid column index. - Each column is also **unbounded vertically**: it can hold any number of pieces. - Pieces are dropped into a column from above and fall under **gravity**: a piece inserted into column `col` lands on top of the pieces already in that column. If a column currently holds `h` pieces, the new piece occupies row `h` of that column (rows are 0-indexed from the bottom, so the bottom cell of every column is row 0). - Two or more players take part; each player is identified by an integer ID. - A player **wins** as soon as they have `N` of their own pieces in a line of consecutive cells, either: - **vertically** — `N` consecutive pieces stacked in the same column, or - **horizontally** — `N` pieces in the same row across `N` consecutive columns. Note that because columns fill independently, adjacent columns can have very different heights (the board is sparsely occupied). A horizontal line at row `r` only counts a column if that column actually has a piece at row `r` and that piece belongs to the player — an empty cell or an opponent's piece breaks the run. ## Task Implement a game class supporting: - `ConnectN(n)` — initialize the game where a player needs `n` in a row to win. - `insert(col, player)` — drop a piece for `player` into column `col`, and return `true` if this move gives `player` `N` in a row (vertically or horizontally), otherwise `false`. You will be given a sequence of moves and must report, for each move, whether it wins the game. Process every move in the sequence even after a win has occurred (each move is judged independently by the rule above). ## Performance Requirement The board is unbounded, so scanning the whole board after every move is not acceptable. Each `insert` must only examine the cells that the new piece could possibly affect: the top of its own column and the cells within distance `N - 1` of the new piece in its row. Your solution should handle up to $10^5$ moves efficiently, with each move costing $O(N)$ time (or better) and total memory proportional to the number of pieces placed. ## Input Format - The first line contains two integers `n` and `m` — the win length ($2 \le n \le 1000$) and the number of moves ($1 \le m \le 10^5$). - Each of the next `m` lines contains two integers `col` and `player` — the column ($-10^9 \le col \le 10^9$) and the player ID ($1 \le player \le 10^9$). ## Output Format For each move, in order, output `true` if that move creates a run of at least `n` consecutive pieces (vertically or horizontally) for the moving player, and `false` otherwise — one value per line. ## Example Input: ``` 3 7 0 1 1 2 1 1 2 2 -1 1 2 1 -1 1 ``` Output: ``` false false false false false false false ``` Walkthrough of the final board state (rows shown bottom-up): ``` row 1: . 1 1 row 0: 1 1 2 2 col: -1 0 1 2 ``` - Move 5 (`-1, 1`): player 1 now has pieces at row 0 in columns -1 and 0, but column 1 holds player 2's piece at row 0, so the horizontal run is only 2 — not a win for `n = 3`. - Move 6 (`2, 1`): the piece lands at row 1 of column 2. In row 1, only columns 1 and 2 are occupied by player 1 — a run of 2, not a win. Column 2 vertically holds `[2, 1]` — no vertical run either. A second example where a win occurs: Input: ``` 3 5 0 1 0 1 5 2 0 1 5 2 ``` Output: ``` false false false true false ``` The fourth move stacks player 1's third consecutive piece in column 0 (rows 0, 1, 2), completing a vertical run of length 3.

Related Interview Questions

  • Collapsible Code Editor: Brace Matching and Toggle - Jane Street (medium)
  • Implement a Circular Buffer - Jane Street (medium)
  • Code Editor with Block Shrink and Expand (Code Folding) - Jane Street (medium)
  • Optimize trade PnL table updates - Jane Street (hard)
  • Transform sparse time-code stream to dense rows - Jane Street (easy)
|Home/Coding & Algorithms/Jane Street

Connect-N on an Infinite Board with Gravity

Jane Street logo
Jane Street
Dec 26, 2025, 12:00 AM
mediumMachine Learning EngineerTechnical ScreenCoding & Algorithms
0
0

You are asked to implement a variant of the classic Connect Four game, played on an unbounded board.

Game Rules

  • The board consists of vertical columns indexed by integers. Columns extend infinitely in both horizontal directions , so any integer (including negative values) is a valid column index.
  • Each column is also unbounded vertically : it can hold any number of pieces.
  • Pieces are dropped into a column from above and fall under gravity : a piece inserted into column col lands on top of the pieces already in that column. If a column currently holds h pieces, the new piece occupies row h of that column (rows are 0-indexed from the bottom, so the bottom cell of every column is row 0).
  • Two or more players take part; each player is identified by an integer ID.
  • A player wins as soon as they have N of their own pieces in a line of consecutive cells, either:
    • vertically — N consecutive pieces stacked in the same column, or
    • horizontally — N pieces in the same row across N consecutive columns.

Note that because columns fill independently, adjacent columns can have very different heights (the board is sparsely occupied). A horizontal line at row r only counts a column if that column actually has a piece at row r and that piece belongs to the player — an empty cell or an opponent's piece breaks the run.

Task

Implement a game class supporting:

  • ConnectN(n) — initialize the game where a player needs n in a row to win.
  • insert(col, player) — drop a piece for player into column col , and return true if this move gives player N in a row (vertically or horizontally), otherwise false .

You will be given a sequence of moves and must report, for each move, whether it wins the game. Process every move in the sequence even after a win has occurred (each move is judged independently by the rule above).

Performance Requirement

The board is unbounded, so scanning the whole board after every move is not acceptable. Each insert must only examine the cells that the new piece could possibly affect: the top of its own column and the cells within distance N - 1 of the new piece in its row. Your solution should handle up to 10510^5105 moves efficiently, with each move costing O(N)O(N)O(N) time (or better) and total memory proportional to the number of pieces placed.

Input Format

  • The first line contains two integers n and m — the win length ( 2≤n≤10002 \le n \le 10002≤n≤1000 ) and the number of moves ( 1≤m≤1051 \le m \le 10^51≤m≤105 ).
  • Each of the next m lines contains two integers col and player — the column ( −109≤col≤109-10^9 \le col \le 10^9−109≤col≤109 ) and the player ID ( 1≤player≤1091 \le player \le 10^91≤player≤109 ).

Output Format

For each move, in order, output true if that move creates a run of at least n consecutive pieces (vertically or horizontally) for the moving player, and false otherwise — one value per line.

Example

Input:

3 7
0 1
1 2
1 1
2 2
-1 1
2 1
-1 1

Output:

false
false
false
false
false
false
false

Walkthrough of the final board state (rows shown bottom-up):

row 1:      .  1  1
row 0:  1   1  2  2
col:   -1   0  1  2
  • Move 5 ( -1, 1 ): player 1 now has pieces at row 0 in columns -1 and 0, but column 1 holds player 2's piece at row 0, so the horizontal run is only 2 — not a win for n = 3 .
  • Move 6 ( 2, 1 ): the piece lands at row 1 of column 2. In row 1, only columns 1 and 2 are occupied by player 1 — a run of 2, not a win. Column 2 vertically holds [2, 1] — no vertical run either.

A second example where a win occurs:

Input:

3 5
0 1
0 1
5 2
0 1
5 2

Output:

false
false
false
true
false

The fourth move stacks player 1's third consecutive piece in column 0 (rows 0, 1, 2), completing a vertical run of length 3.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Jane Street•More Machine Learning Engineer•Jane Street Machine Learning Engineer•Jane Street Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.