Quick Overview

Simulate a two-player game in which pieces drop into integer-numbered columns of a board without edges and stack from the bottom, and find the first move that completes a line of k pieces horizontally, vertically or diagonally. It tests modeling an unbounded board and checking every line direction correctly.

First Winning Move in a Gravity Drop Game on an Unbounded Board

Company: Jane Street

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Two players take turns dropping pieces into the columns of a board that has no edges. Columns are identified by any integer (positive, zero or negative), and every column is unbounded upward. Gravity applies: a piece dropped into a column lands on the lowest empty cell of that column, so the first piece in a column occupies row `0`, the next one row `1`, and so on. Player 1 makes the first move and the players then alternate, so move `i` (0-based) belongs to player 1 when `i` is even and to player 2 when `i` is odd. A player wins when the piece just placed completes a line of at least `k` of that player's pieces in consecutive cells, horizontally, vertically or along either diagonal. Given the columns in the order the pieces are dropped and the win length `k`, return the 0-based index of the first move that wins. If no move wins, return `-1`. ### Function Signature ```python def first_winning_move(moves: list[int], k: int) -> int: ``` ### Rules - Cell `(c, r)` is column `c`, row `r`. A line is a set of `k` cells `(c + j*dc, r + j*dr)` for `j = 0, 1, ..., k-1`, where the direction `(dc, dr)` is `(1, 0)`, `(0, 1)`, `(1, 1)` or `(1, -1)`, and every one of those cells holds a piece of the same player. - Rows below `0` do not exist. Columns with different numbers are different columns however far apart they are, and nothing wraps around. - A line longer than `k` also wins: any `k` consecutive cells of it form a line. - The game ends at the first winning move. Moves after it in the list are ignored. ### Constraints - `1 <= len(moves) <= 10^5` - `-10^9 <= moves[i] <= 10^9` - `2 <= k <= 20` - The result is `-1` or an index from `0` to `len(moves) - 1`. ### Examples #### Example 1 ```text Input: moves = [0,1,0,1,0,1,0], k = 4 Output: 6 ``` Player 1 drops into column 0 on moves 0, 2, 4 and 6, filling rows 0 to 3 of that column. Player 2's three pieces in column 1 never reach four. #### Example 2 ```text Input: moves = [-1,0,0,1,5,1,1], k = 3 Output: 6 ``` Player 1's pieces land at `(-1, 0)`, `(0, 1)`, `(5, 0)` and finally `(1, 2)`, on top of player 2's two pieces in column 1. That last piece completes the diagonal `(-1, 0)`, `(0, 1)`, `(1, 2)`. Before move 6 neither player has three in a line: player 2 holds `(0, 0)`, `(1, 0)` and `(1, 1)`. #### Example 3 ```text Input: moves = [0,1000000000,-1000000000,0], k = 2 Output: -1 ``` Player 1 holds `(0, 0)` and `(-1000000000, 0)`, and player 2 holds `(1000000000, 0)` and `(0, 1)`. No two pieces of the same player are in adjacent cells.

Overview: Simulate a two-player game in which pieces drop into integer-numbered columns of a board without edges and stack from the bottom, and find the first move that completes a line of k pieces horizontally, vertically or diagonally. It tests modeling an unbounded board and checking every line direction correctly.

Two players take turns dropping pieces into the columns of a board that has no edges. Columns are identified by any integer (positive, zero or negative), and every column is unbounded upward. Gravity applies: a piece dropped into a column lands on the lowest empty cell of that column, so the first piece in a column occupies row `0`, the next one row `1`, and so on. Player 1 makes the first move and the players then alternate, so move `i` (0-based) belongs to player 1 when `i` is even and to player 2 when `i` is odd. A player wins when the piece just placed completes a line of at least `k` of that player's pieces in consecutive cells, horizontally, vertically or along either diagonal. Given `moves`, the columns in the order the pieces are dropped, and the win length `k`, return the 0-based index of the first move that wins. If no move wins, return `-1`. ### Rules - Cell `(c, r)` is column `c`, row `r`. A line is a set of `k` cells `(c + j*dc, r + j*dr)` for `j = 0, 1, ..., k-1`, where the direction `(dc, dr)` is `(1, 0)`, `(0, 1)`, `(1, 1)` or `(1, -1)`, and every one of those cells holds a piece of the same player. - Rows below `0` do not exist. Columns with different numbers are different columns however far apart they are, and nothing wraps around. - A line longer than `k` also wins: any `k` consecutive cells of it form a line. - The game ends at the first winning move. Moves after it in the list are ignored. ### Example 1 ```text Input: moves = [0,1,0,1,0,1,0], k = 4 Output: 6 ``` Player 1 drops into column 0 on moves 0, 2, 4 and 6, filling rows 0 to 3 of that column. Player 2's three pieces in column 1 never reach four. ### Example 2 ```text Input: moves = [-1,0,0,1,5,1,1], k = 3 Output: 6 ``` Player 1's pieces land at `(-1, 0)`, `(0, 1)`, `(5, 0)` and finally `(1, 2)`, on top of player 2's two pieces in column 1. That last piece completes the diagonal `(-1, 0)`, `(0, 1)`, `(1, 2)`. Before move 6 neither player has three in a line: player 2 holds `(0, 0)`, `(1, 0)` and `(1, 1)`. ### Constraints - `1 <= len(moves) <= 10^5` - `-10^9 <= moves[i] <= 10^9` - `2 <= k <= 20` - The result is `-1` or an index from `0` to `len(moves) - 1`. Every column id and every returned index fits in a signed 32-bit integer.

Constraints

  • 1 <= len(moves) <= 10^5
  • -10^9 <= moves[i] <= 10^9
  • 2 <= k <= 20
  • The result is -1 or an index from 0 to len(moves) - 1.

Examples

Input: ([0], 2)

Expected Output: -1

Explanation: A single piece cannot form a line because k is at least 2.

Input: ([0, 1, 0], 2)

Expected Output: 2

Explanation: Player 1 stacks (0, 0) and (0, 1), the smallest vertical win with k = 2.

Hints

  1. A win is defined through the piece just placed, so look at the lines that pass through that one cell.
  2. Column ids can be negative and up to 2 * 10^9 apart, so store only the cells that hold pieces.
  3. Because of gravity, the row of a new piece equals the number of pieces already in its column.

First Winning Drop on an Edgeless Gravity Board

mediumJane StreetSoftware Engineer

Two players take turns dropping pieces into the columns of a board that has no edges. Columns are identified by any integer (positive, zero or negative), and every column is unbounded upward. Gravity applies: a piece dropped into a column lands on the lowest empty cell of that column, so the first piece in a column occupies row 0, the next one row 1, and so on.

Player 1 makes the first move and the players then alternate, so move i (0-based) belongs to player 1 when i is even and to player 2 when i is odd. A player wins when the piece just placed completes a line of at least k of that player's pieces in consecutive cells, horizontally, vertically or along either diagonal.

Given moves, the columns in the order the pieces are dropped, and the win length k, return the 0-based index of the first move that wins. If no move wins, return -1.

Rules

  • Cell (c, r) is column c, row r. A line is a set of k cells (c + j*dc, r + j*dr) for j = 0, 1, ..., k-1, where the direction (dc, dr) is (1, 0), (0, 1), (1, 1) or (1, -1), and every one of those cells holds a piece of the same player.
  • Rows below 0 do not exist. Columns with different numbers are different columns however far apart they are, and nothing wraps around.
  • A line longer than k also wins: any k consecutive cells of it form a line.
  • The game ends at the first winning move. Moves after it in the list are ignored.

Example 1

Input:  moves = [0,1,0,1,0,1,0], k = 4
Output: 6

Player 1 drops into column 0 on moves 0, 2, 4 and 6, filling rows 0 to 3 of that column. Player 2's three pieces in column 1 never reach four.

Example 2

Input:  moves = [-1,0,0,1,5,1,1], k = 3
Output: 6

Player 1's pieces land at (-1, 0), (0, 1), (5, 0) and finally (1, 2), on top of player 2's two pieces in column 1. That last piece completes the diagonal (-1, 0), (0, 1), (1, 2). Before move 6 neither player has three in a line: player 2 holds (0, 0), (1, 0) and (1, 1).

Constraints

  • 1 <= len(moves) <= 10^5
  • -10^9 <= moves[i] <= 10^9
  • 2 <= k <= 20
  • The result is -1 or an index from 0 to len(moves) - 1.

Every column id and every returned index fits in a signed 32-bit integer.

Examples

Example 1
Input
([0], 2)
Output
-1
Notes
A single piece cannot form a line because k is at least 2.
Example 2
Input
([0, 1, 0], 2)
Output
2
Notes
Player 1 stacks (0, 0) and (0, 1), the smallest vertical win with k = 2.

Constraints

  • •1 <= len(moves) <= 10^5
  • •-10^9 <= moves[i] <= 10^9
  • •2 <= k <= 20
  • •The result is -1 or an index from 0 to len(moves) - 1.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def first_winning_move(moves: list[int], k: int) -> int:
Sample call
first_winning_move([0], 2)
Language
Case 1
Input
([0], 2)
Expected
-1
Case 2
Input
([0, 1, 0], 2)
Expected
2

Browse More Questions

Show the approach

Approach

Keep a map from each column that has received a piece to the list of owners in that column, from row 0 upward. The length of a column's list is its height, so a piece dropped into column c lands at row r = height(c), and move i belongs to player i % 2.

Before move i no line exists, since the game would already have ended, and every cell except (c, r) is unchanged. So move i wins exactly when some line through (c, r) holds k pieces of the mover. For each of the four directions (1, 0), (0, 1), (1, 1) and (1, -1), start a count at 1 for the new piece, walk forward and then backward while the next cell holds the mover's piece, and stop as soon as the count reaches k. The count measures the run of the mover's pieces through (c, r) along that direction, capped at k, so it reaches k if and only if some k consecutive cells of that run form a line. Return i at the first direction that reaches k; if no move wins, return -1.

A cell (x, y) is empty when y is negative, when column x has no list, or when y is at least the height of column x. Rejecting a negative row before indexing matters in Python, where a negative list index would read the top of the column. Edge cases: a single move never wins because k is at least 2; columns that are far apart never touch and nothing wraps; moves after the first win are never processed. Each move inspects at most 8 * (k - 1) cells with hash lookups, giving O(n * k) time and O(n) space for n moves.

Time complexity:
O(n * k)
Space complexity:
O(n)