Place fixed shapes on a grid in order
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's online assessment, last asked Sep 2025. It tests simulation and grids.
You get an empty n x m grid and a list of tile patterns. Each pattern is a small binary matrix, where 1 is a filled cell and 0 is a hole, and it is placed exactly as given, with no rotation or reflection. Process the patterns in order. For pattern i, scan anchor positions in row-major order (top to bottom, then left to right) and place it at the first position where all its filled cells lie inside the grid and cover only empty cells. Write i + 1 into every covered cell. If no position fits, skip the pattern. Return the final grid, with . for empty cells.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
place_tiles(n: int, m: int, patterns: list[list[list[int]]]) -> list[str]— one string per row, with each placed pattern's cells holding its 1-based number (assume fewer than 10 patterns so each cell is one character, or agree on a separator). The anchor is the pattern's top-left corner and runs over every grid cell. Only filled cells must be inside the grid and on empty cells: holes may cover occupied cells or hang past the border.
Example
patterns = [
[[1, 1],
[1, 0]], # 1
[[1, 1, 1]], # 2
[[0, 1],
[1, 1]], # 3
[[1, 1, 1, 1, 1]], # 4
]
place_tiles(3, 4, patterns) -> ["11..",
"1222",
"...."]
# 2 does not fit in row 0 (only two cells are free), so it goes to row 1 at column 1
# 3 never fits: every placement needs a free cell in row 1 or a fourth row
# 4 is wider than the grid
Constraints
-
1 <= n, m <= 50. A pattern may be larger than the grid. - Every pattern has at least one filled cell.
Clarifying questions
- May a pattern's holes (
0s) lie outside the grid, or must the whole bounding box fit? (Here they may.) - Is the scan order over the pattern's top-left corner or over its first filled cell?
- Should the caller learn which patterns were skipped?
- Are patterns given as matrices or as strings?
What a strong answer covers
- Precomputing each pattern's list of filled offsets, so holes cost nothing and the fit check only looks at filled cells.
- The scan with an early exit on the first conflict, and stopping at the first fitting anchor.
- Cost: O(n · m · c) per pattern in the worst case, where
cis its filled-cell count. - Explicit rules for the scan order, tie-breaking and holes, stated before coding.
- Tests: a pattern larger than the grid, a pattern with holes that interlock with an earlier one, a full grid.
Follow-ups
- Allow the four rotations, trying them in a fixed order at each anchor.
- Make the fit check faster with a 2D prefix sum of occupied cells for solid rectangular patterns.
- Patterns fall from the top like Tetris pieces instead of being placed at the first fitting position.
Where it's asked
- Uber · Online assessment · Last asked Sep 202590 min · Two problems on a HackerRank-based platform, camera and screen share on · 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.
