Board Simulation: Bottom-Insert Pieces with Uniform Row and Column Detection
Jane StreetMachine Learning Engineer · Technical ScreenLast updated
This question evaluates the ability to simulate a 2-D game board and maintain dynamic state invariants, testing array manipulation, state-tracking, and detection of uniform rows and columns under bottom-insert semantics.
Board Simulation: Bottom-Insert Pieces with Uniform Row and Column Detection
You must simulate a 2-D game board with R rows and C columns. Each cell is either empty or holds a piece of one of two types, "A" or "B". Rows are numbered from 0 (the bottom row) to R - 1 (the top row); columns are numbered 0 to C - 1.
Insertion rule
Pieces are always inserted at the bottom of a column:
- When a piece
pis inserted into columnc, every piece already in columncshifts up by one row, and the new piece occupies the bottom cell(row 0, column c). - Because pieces only enter from the bottom, the pieces in any column always occupy a contiguous block of rows starting at row
0. - If column
calready holdsRpieces (the column is full), the insertion fails and the board state does not change.
Task
Process a sequence of insertion operations. Each operation is a pair (c, p) meaning "insert a piece of type p into column c". After processing each operation (whether it succeeded or failed), report three values describing the board state at that moment:
ok—trueif the insertion succeeded,falseif it failed because columncwas already full.uniform_column—trueif any column is completely filled (allRcells occupied) with pieces of the same type; otherwisefalse.uniform_row—trueif any row is completely filled (allCcells in that row occupied) with pieces of the same type; otherwisefalse.
Return the list of these triples, one per operation, in order.
Input
- An integer
R— the number of rows (1 <= R <= 50). - An integer
C— the number of columns (1 <= C <= 50). - A list
opsof operations, where each operation is[c, p]with0 <= c < Candpequal to the string"A"or"B"(1 <= len(ops) <= 10^4).
Output
A list of len(ops) triples [ok, uniform_column, uniform_row] (booleans), where the i-th triple describes the board immediately after processing the i-th operation.
Example
Input:
R = 2, C = 2
ops = [[0, "A"], [1, "A"], [0, "B"], [1, "B"], [1, "A"]]
Output:
[[true, false, false],
[true, false, true],
[true, false, false],
[true, false, true],
[false, false, true]]
Explanation (rows listed bottom-up; . = empty):
add(0, "A"): column 0 becomes[A]. Board: row 0 =A .. No full column; row 0 is not fully occupied. ->[true, false, false]add(1, "A"): column 1 becomes[A]. Row 0 =A A— fully occupied, all typeA, so a uniform row exists. Neither column is full. ->[true, false, true]add(0, "B"): the newBenters at the bottom of column 0 and pushes the existingAup, so column 0 is[B, A](row 0 =B, row 1 =A). Column 0 is full but mixed. Row 0 =B A(mixed); row 1 =A .(not fully occupied). ->[true, false, false]add(1, "B"): column 1 becomes[B, A]. Both columns are full but mixed. Row 0 =B B(uniform); row 1 =A A(uniform). ->[true, false, true]add(1, "A"): column 1 is full, so the insertion fails and the board is unchanged. ->[false, false, true]
Notes
- A column counts as uniform only when all
Rof its cells are occupied by the same piece type; a partially filled column never counts, even if its current pieces all match. - A row counts as uniform only when all
Cof its cells are occupied by the same piece type. Rowris fully occupied exactly when every column currently holds more thanrpieces. - Failed insertions must leave the board — and therefore the two uniformity flags — unchanged from the previous state.
- A brute-force
O(R * C)scan after each operation is acceptable at these constraints, but an incremental solution that updates per-column and per-row bookkeeping inO(R)or better per operation is preferred.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.