Design a 3D Binary Matrix with Efficient Layer Tracking
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a 3D binary matrix of size `n x n x n` where every cell is initially `0`. The matrix supports flipping individual cells on and off, as well as querying whether any **z-layer** (a 2D slice with a fixed `z` coordinate) is completely filled with `1`s.
A z-layer `z` is the set of all cells `(x, y, z)` for `0 <= x, y < n`. The layer is considered **completely filled** when all `n²` cells in that layer equal `1`.
All update and query operations must run efficiently, ideally in `O(1)` time per call.
The constructor `Matrix3D(n)` creates the matrix with every cell set to `0`. Implement the following methods:
- `setCell(x: int, y: int, z: int)`: Set the cell at coordinates `(x, y, z)` to `1`. No-op if it is already `1`.
- `unsetCell(x: int, y: int, z: int)`: Set the cell at coordinates `(x, y, z)` to `0`. No-op if it is already `0`.
- `largestMatrix() -> int`: Return `1` if there exists at least one z-layer completely filled with `1`s, otherwise return `0`.
Example 1:
```text
Input:
["Matrix3D", "setCell", "setCell", "setCell", "setCell", "largestMatrix", "unsetCell", "largestMatrix"]
[[2], [0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 0], [], [0, 0, 0], []]
Output: [null, null, null, null, null, 1, null, 0]
Explanation:
matrix = Matrix3D(2) # 2x2x2 grid, all zeros
matrix.setCell(0, 0, 0) # layer z=0 count: 1
matrix.setCell(0, 1, 0) # layer z=0 count: 2
matrix.setCell(1, 0, 0) # layer z=0 count: 3
matrix.setCell(1, 1, 0) # layer z=0 count: 4 (full)
matrix.largestMatrix() # -> 1 (z=0 layer is completely filled)
matrix.unsetCell(0, 0, 0) # layer z=0 count: 3 (no longer full)
matrix.largestMatrix() # -> 0 (no layer is completely filled)
```
Example 2:
```text
Input:
["Matrix3D", "largestMatrix", "setCell", "setCell", "largestMatrix", "unsetCell", "largestMatrix"]
[[1], [], [0, 0, 0], [0, 0, 0], [], [0, 0, 0], []]
Output: [null, 0, null, null, 1, null, 0]
Explanation:
matrix = Matrix3D(1) # 1x1x1 grid (single cell)
matrix.largestMatrix() # -> 0 (cell is 0)
matrix.setCell(0, 0, 0) # cell becomes 1, layer z=0 full
matrix.setCell(0, 0, 0) # already 1, no change
matrix.largestMatrix() # -> 1 (layer z=0 is full)
matrix.unsetCell(0, 0, 0) # cell becomes 0
matrix.largestMatrix() # -> 0 (no full layer)
```
```hint Avoid rescanning
Checking a layer cell by cell costs `n²` per query. Each update touches one cell in one layer, so ask what small summary of each layer it can keep current.
```
```hint Repeated calls
Calling `setCell` twice on the same cell must leave every counter where one call left it. Decide what you need to store to detect that.
```
### Constraints & Assumptions
- `1 <= n <= 100`
- `0 <= x, y, z < n`
- At most `10⁵` calls in total to `setCell`, `unsetCell`, and `largestMatrix`.
### What a Strong Answer Covers
- What state lets `largestMatrix` answer without scanning the matrix
- How repeated `setCell` or `unsetCell` calls on the same cell are kept from changing the counts
- Exactly when the number of full layers goes up or down, including the order of the check and the update in `unsetCell`
- Time per call and memory for `n` up to 100
### Follow-up Questions
- How would you return how many z-layers are full, or the index of one of them?
- How would you also report full x-layers and y-layers?
- If `n` were large and only a small fraction of cells were ever set, how would you change the storage?
- What has to change if several threads update the matrix at once?
Overview: Design an n by n by n binary matrix that supports setting and unsetting single cells and reports whether any z-layer is entirely filled with ones. Tests incremental counting, idempotent updates, and keeping a derived counter correct so every operation runs in constant time.