← Questions
Sparse canvas painting with undo and redo
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Figma's technical phone screen, last asked Sep 2023. It tests hash maps, stacks and undo-redo.
Build an unbounded pixel canvas that remembers its paint history. Painting sets one pixel's color, reading returns a pixel's current color, and undo and redo walk back and forth through the paints. Coordinates can be anywhere in a huge range, so store only the pixels that were painted.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
paint(x: int, y: int, color: int) -> Nonesets pixel(x, y)tocolor. Every paint is one history entry, even one that writes the color the pixel already has. A paint clears the redo history. -
read(x: int, y: int) -> intreturns the pixel's current color, or0if it has never been painted (or every paint of it was undone). -
undo() -> Nonereverts the most recent paint that is not already undone. With nothing to undo, it does nothing. -
redo() -> Nonereapplies the most recently undone paint. With nothing to redo, it does nothing.
Example
read(1, 1) -> 0
paint(1, 1, 5)
read(1, 1) -> 5
undo()
read(1, 1) -> 0
redo()
read(1, 1) -> 5
paint(2, 3, 7)
paint(2, 3, 9)
read(2, 3) -> 9
undo()
read(2, 3) -> 7 # the earlier paint shows again
undo()
read(2, 3) -> 0
paint(0, 0, 1)
undo()
paint(4, 4, 2) # clears the redo history
redo() # nothing to redo
read(0, 0) -> 0
read(4, 4) -> 2
Constraints
- Up to 200,000 operations.
-
-10^9 <= x, y <= 10^9; every paintedcoloris between 1 and 10^9. - Each operation should take O(1) time on average.
Clarifying questions
- Does a paint that writes the current color count as its own history entry? (Yes.)
- After undo brings a pixel back to "never painted", should it still take memory?
- Is the history unbounded, or should it keep only the last N paints?
- Do reads ever need to cover a rectangle rather than one pixel?
What a strong answer covers
- A hash map keyed by
(x, y)for the pixels, and two stacks for undo and redo. - Each history entry stores the pixel, its color before the paint and the color written, so undo and redo are O(1) and never search the history.
- A paint clears the redo stack; undo and redo on an empty stack do nothing.
- Removing the map entry when a pixel goes back to color 0, so memory tracks only painted pixels.
- Tests: repeated paints of one pixel, undo past the start, redo after a new paint, far-apart and negative coordinates.
Follow-ups
- Add a stroke that paints many pixels and is undone or redone as one step.
- Cap the history at the last N entries without making undo slower.
- Support
read_rect(x1, y1, x2, y2), which returns every painted pixel in a rectangle.
Where it's asked
- Figma · Technical phone screen · Last asked Sep 202360 min · Pair programming in your own language against tests the interviewer provides
What it tests
hash maps · stacks · undo-redo
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.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
