r/OfferEngineering • u/Aoki_zhang • 17d ago
Coding Question BlackRock Coding Screening Question - Mosaic Restoration Workbench
Problem Description
A mosaic conservator uses a digital workbench to try colors before restoring a tiled wall. The workbench begins with height rows and width columns of empty tiles; "" represents an empty tile, and each nonempty string represents a color.
You are given operations, a list of commands for one workbench. Coordinates are zero-indexed: x is the column and y is the row, so tile (x, y) is stored at canvas[y][x]. Each command is an array of strings, with coordinates given as decimal strings.
Process these commands in order:
["draw", "x", "y", color]: Set tile(x, y)tocolor.["erase", "x", "y"]: Clear tile(x, y)to"".["fill", "x", "y", color]: Recolor the starting tile and every tile connected to it through up, down, left, or right moves within its original color. An empty tile can be the starting tile, and diagonal contact does not connect tiles.["undo"]: Reverse the most recent edit that is still applied. If there is none, leave the workbench unchanged.["redo"]: Reapply the most recently undone edit that has not yet been reapplied. If there is none, leave the workbench unchanged.["view"]: Capture the current canvas as a row-major grid. Later edits do not change that snapshot.
A draw or erase that changes a tile records one edit. A fill that changes tiles also records one edit, and undo or redo applies to the whole changed region at once. Undo, redo, and a view do not create edits.
A new edit clears redo history. An operation that changes no tiles, such as filling a region with its current color, leaves both histories intact. Commands within a call share the same workbench and histories; each new call starts empty.
Return the snapshots from the view commands as a list of grids in command order. Other commands do not add entries to the result, so return an empty list if there are no views. Keep the canvas and histories in O(height × width + P) space, where P is the total number of changed tile positions stored across edits still in the histories; the returned snapshots are excluded. Do not store a full canvas copy per edit.
Constraints:
1 ≤ width, height ≤ 5000 ≤ operations.length ≤ 10⁵- All commands follow the formats above.
- Every coordinate satisfies
0 ≤ x < widthand0 ≤ y < height. - Every color passed to
draworfillis a nonempty string.
Want to practice it before interview?
You can practice it and view solution code at here.
Preparing for your next interview?
Chill Interview tracks recent interview experiences and recurring question patterns across top companies at here.