As reported: Flexport SWE-1 (Bangalore, 2025), DSA round — "Infinite Grid Game": players drop colored balls (colors
1..n) from the top; a player who gets 3 balls of the same color in a column wins. Follow-up 1: win with 3 in a row or 3 in a column. Follow-up 2: generalize tokin a row orkin a column. Diagonals were discussed but not coded.
The grid has unboundedly many columns (any integer index, negative too) and unbounded height. A dropped ball falls to the top of its column's stack — row 0 is the bottom. Implement:
BallGame(k)—kballs of one color in a line win.drop(column, color)→trueif, after placing this ball, it is part ofkor more consecutive balls of its color vertically in its column or horizontally in its row (same row in neighbouring columns); otherwisefalse. The game continues after a win.
Rows are compared by height: in a row, a column that is shorter than the row has a gap there.
Example 1 (k = 3)
drop(0, 1) -> false
drop(0, 1) -> false
drop(1, 2) -> false
drop(0, 1) -> true (column 0 holds 1, 1, 1)
Example 2 (k = 3)
drop(5, 2) -> false
drop(6, 2) -> false
drop(-100, 1) -> false
drop(4, 2) -> true (row 0: columns 4, 5, 6 are all color 2)
Constraints: 1 ≤ k ≤ 10, up to 2·10^4 drops, |column| ≤ 10^9, 1 ≤ color ≤ 10^4.
Code runner format: line 1 is k, line 2 the drops as [column, color] pairs. The runner prints the result of every drop.