← Questions

Drop a Tetris piece and clear full rows

0

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: an H x W grid of 0/1. 1 is an occupied cell, 0 is empty. Row 0 is the top.
  • block: an h x w grid of 0/1 that describes the falling piece. A 1 is part of the piece. A 0 is 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 of block) 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:

  1. Lock it into the board (its filled cells become 1).
  2. 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.
  3. 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 None when 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding