Design paint editor with undo/redo
BlackRockSoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's ability to implement mutable state management, efficient data structures and algorithms for pixel operations (draw, erase, flood fill) alongside correct undo/redo history tracking and state consistency.
Implement a simplified Paint editor that supports drawing on a 2D canvas and reverting changes using undo and redo operations.
Canvas
You are given a 2D grid representing a canvas. Each cell can store:
- a color value (string or integer), or
Nonefor an empty cell.
You will initialize the canvas with a given width and height.
Assume coordinates are zero-based and given as (x, y) where x is the column index and y is the row index.
Coordinates are guaranteed to be within bounds.
Required Operations
You must design and implement a Paint class with the following methods:
draw(x, y, color)
- Set the pixel at position
(x, y)to the givencolor.
erase(x, y)
- Remove whatever color is at
(x, y)and set it back to empty (None).
fill(x, y, color)
- Perform a flood fill operation starting at
(x, y): - Let
origbe the color currently at(x, y). - Replace the pixel at
(x, y)withcolor. - Recursively replace any adjacent cells (up, down, left, right) that have color
origwithcolor. - Stop expanding when encountering a different color or the canvas boundary.
- If
origis already equal tocolor, the operation may be a no-op.
undo()
- Revert the canvas to the state before the most recent modifying operation (
draw,erase, orfill). - If there is no operation to undo, this may be a no-op.
redo()
- Re-apply the most recently undone operation, restoring the canvas to the state after that operation.
- If there is no operation to redo, this may be a no-op.
Behavior Rules
- Any operation that modifies the canvas (
draw,erase,fill) should become an undoable action. - Calling
undo()moves the current state into a redo history so that it can be restored withredo(). - Calling
redo()re-applies that state and moves it back into the undo history. - After performing a new modifying operation (
draw,erase,fill), the redo history must be cleared. - All operations should be deterministic and consistent.
Requirements
- Design and implement the
Paintclass and its methods. - You may choose any reasonable data structures for representing:
- The canvas.
- Undo and redo histories.
- Your implementation must correctly maintain canvas state, undo/redo history, and fill behavior.
Assume reasonable constraints, e.g., canvas up to a few thousand by a few thousand cells and up to tens of thousands of operations.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.