Drop a Tetris piece and clear full rows
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Databricks. It tests simulation, matrix and game.
Implement a single step of a Tetris-like game.
You get three inputs:
-
background: anH x Wgrid of0/1.1is an occupied cell,0is empty. Row 0 is the top. -
block: anh x wgrid of0/1that describes the falling piece. A1is part of the piece. A0is empty space inside the piece's bounding box and never collides with anything. -
offset: the zero-indexed board column where the piece's left edge (column 0 ofblock) is aligned.
The piece starts completely above the board and falls straight down. It does not move sideways or rotate. It stops at the last position where none of its filled cells overlaps an occupied cell or goes below the bottom row.
Implement:
def drop_block(background: list[list[int]], block: list[list[int]], offset: int) -> list[list[int]] | None
When the piece stops:
- Lock it into the board (its filled cells become
1). - Remove every row that is now completely full. Rows above a removed row move down; empty rows are added at the top so the board keeps its size.
- Return the new board. Do not modify the input.
If the piece comes to rest with any filled cell still above row 0, it does not fit: return None.
Constraints: 1 <= H, W <= 1000, 1 <= h, w <= 10, 0 <= offset <= W - w, and block has at least one 1.
Example 1
background = [[0,0,0],
[0,0,0],
[0,1,0],
[1,1,0]]
block = [[1,1],
[0,1]]
offset = 1
The piece's left column stops on the 1 at row 2, so its top row lands on row 1 and its lower cell on row 2. Row 2 becomes [0,1,1], not full. Result:
[[0,0,0],
[0,1,1],
[0,1,1],
[1,1,0]]
Example 2
background = [[0,0,0],
[0,0,0],
[1,0,1]]
block = [[1,1,1],
[0,1,0]]
offset = 0
The stem drops into the gap. Rows 1 and 2 both become full and are removed. Result: [[0,0,0],[0,0,0],[0,0,0]].
Start with a correct simulation, then make the landing step faster than moving the piece one row at a time.
Clarifying questions
- Can a piece's filled cells end up above row 0 while its lowest row is on the board? Does that count as "does not fit"?
- Can the piece have empty rows or columns at the edges of its bounding box (for example a bottom row of zeros)?
- When several rows clear at once, do they all clear together, with no chain of further clears?
- Is the board a list of lists I may copy, or should the function return a new structure of the same shape?
What a strong answer covers
- Simulation first: start the piece above the board, move it down one row while the next position is free, and lock it in. That is O(H * h * w).
- Faster landing: for each piece column, find the lowest filled cell of the piece and the highest occupied board cell below where it enters, and take the minimum gap over the columns. That makes the landing O(w * H) in the worst case and O(w) with a column-height array. It also has to handle overhangs, where a column of the board has empty cells under an occupied one that the piece can't reach.
- Locks the piece into a copy of the board, finds full rows among the rows the piece touched (only those can have become full), removes them, and adds empty rows at the top.
- Returns
Nonewhen any filled cell rests above row 0, and never modifies the input. - Tests: both examples, an empty board, a piece that clears no rows, a piece with zero cells in its bounding box, a piece that does not fit, and an overhang where the piece stops on a higher cell.
Follow-ups
- Allow rotation and a horizontal shift before the drop, and pick the placement that clears the most rows.
- Run a whole game from a list of pieces and report the score.
- Keep column heights up to date across many drops so each landing is O(w).
Where it's asked
What it tests
simulation · matrix · game
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
