Place Tetris-like shapes in the first spot that fits
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Meta's CodeSignal assessment, last asked Feb 2026. It tests simulation and grids.
Build a simulator for a Tetris-like puzzle. You have an empty grid of n rows and m columns (row 0 at the top) and a sequence of pieces, each one of five types A to E. Each type is a fixed shape. Place the pieces one at a time, in order. A piece cannot be rotated or flipped, must lie fully inside the grid, and cannot overlap a cell that is already filled. Put each piece at the first position where it fits, trying top-left anchors by smallest row first and, within a row, smallest column first. When a piece fits nowhere, stop.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
simulate(n: int, m: int, shapes: dict[str, list[tuple[int, int]]], pieces: list[str]) -> tuple[int, list[str]]—shapes[t]lists the(row, col)offsets of a piece's cells relative to its anchor, the top-left corner of the piece's bounding box. Returns how many pieces were placed and the final grid, one string per row, with.for an empty cell and the piece's letter for a filled one.
Example
shapes = {
"A": [(0, 0)], # one cell
"B": [(0, 0), (0, 1)], # horizontal domino
"C": [(0, 0), (1, 0)], # vertical domino
"D": [(0, 0), (0, 1), (1, 0), (1, 1)], # 2 x 2 square
"E": [(0, 1), (1, 0), (1, 1)], # L missing its top-left cell
}
simulate(3, 4, shapes, ["D", "B", "E", "C", "A", "A"])
-> (5, ["DDBB",
"DDEC",
"AEEC"])
D goes to anchor (0, 0) and B to (0, 2). E does not fit at any anchor in row 0, so it goes to anchor (1, 1): its empty top-left corner sits over a D cell, which is allowed. C goes to (1, 3) and A to (2, 0). The grid is now full, so the second A fits nowhere and the simulation stops with 5 pieces placed.
Constraints
-
1 <= n, m <= 100; up to10^4pieces. - Each shape has at most 5 cells inside a small bounding box.
Clarifying questions
- What should be returned: the final grid, the number of pieces placed, or both?
- When a piece fits nowhere, do we stop, or skip it and continue with the next?
- Is the anchor the top-left of the bounding box, or a specific cell of the piece?
- Do full rows clear, as in real Tetris?
What a strong answer covers
- A clean model: shapes as offset lists, the grid as a 2D array, and a
fits(shape, r, c)check for bounds and overlaps. - The scan in the right priority order, placing at the first fit and stopping or continuing as agreed.
- Cost:
O(n * m * s)per piece forscells per shape, and how to speed it up, for example by remembering the first row that still has free cells. - Tests: a piece larger than the grid, a piece whose bounding box has empty corners, a grid that fills exactly, a piece that fits only on the last row.
Follow-ups
- Clear full rows as in Tetris and shift everything above them down.
- Allow rotations, choosing the first rotation that fits at the earliest anchor.
- Pieces drop from the top of a chosen column instead of being placed anywhere. How does the simulation change?
Where it's asked
- Meta · CodeSignal assessment · Last asked Feb 202690 min · Four levels on one class: basic operations, a ranking query, timestamps and expiry, then merge or snapshot and restore · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
