KatInterview

Canvas Replay with Undo and Redo History

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designstacksimulationflood-fill

Last updated

Simulate a pixel canvas with draw, erase, flood fill, undo and redo, returning a canvas snapshot after every command while keeping the history stacks consistent.

You are building the engine of a tiny drawing program. The canvas is a grid with height rows and width columns. Every cell is either empty (None) or holds a color, which is a string or an integer. A new canvas starts with every cell empty.

A cell is addressed as (x, y), where x is the column and y is the row, both zero-based. In Python the cell is canvas[y][x]. All coordinates are within the canvas.

You are given a list of commands and a parallel list of their arguments. Replay the commands in order and return the state of the canvas after each command.

Commands

CommandArgumentsEffect
draw[x, y, color]Set cell (x, y) to color.
erase[x, y]Set cell (x, y) to empty (None).
fill[x, y, color]Flood fill, described below.
undo[]Go back to the canvas as it was before the most recent change that is currently applied.
redo[]Re-apply the change most recently undone.

Flood fill

Let original be the value currently in cell (x, y) (it may be None).

  • If original equals color, nothing changes on the canvas.
  • Otherwise, every cell that holds original and can be reached from (x, y) by moving up, down, left or right through cells that all hold original is set to color. Diagonal moves do not count, and the fill never leaves the canvas.

History rules

  • draw, erase and fill are modifying commands. Each one adds an entry to the undo history and empties the redo history. This happens always, even if the command leaves the canvas unchanged (for example drawing a color already present, erasing an empty cell, or a fill whose original equals color). A later undo will then step back to an identical canvas.
  • undo with a non-empty undo history moves the current canvas into the redo history and restores the canvas from the most recent undo entry. If the undo history is empty, the canvas is unchanged.
  • redo with a non-empty redo history moves the current canvas into the undo history and restores the most recently saved redo entry. If the redo history is empty, the canvas is unchanged.
  • undo and redo themselves are not modifying commands, so they do not clear the redo history.

Output

Return a list with one entry per command, in order. Each entry is a snapshot of the canvas (a height x width list of lists, indexed [y][x]) taken right after that command, including undo and redo commands and ones that did nothing. Snapshots must be independent copies, so later commands do not alter earlier snapshots.

Function

canvas_history(width, height, commands, args) returns the list of snapshots.

Constraints

  • 1 <= width, height; the canvas may be large (up to a few thousand cells per side).
  • Up to tens of thousands of commands.
  • len(commands) == len(args).
  • Command names are one of draw, erase, fill, undo, redo.
  • All coordinates are in bounds.
  • Colors are strings or integers and are compared with ==.

Examples

Example 1

Input: canvas_history(3, 2, ['draw', 'draw', 'fill', 'undo', 'redo'], [[0, 0, 'red'], [1, 0, 'red'], [0, 0, 'blue'], [], []])

Output: [[['red', None, None], [None, None, None]], [['red', 'red', None], [None, None, None]], [['blue', 'blue', None], [None, None, None]], [['red', 'red', None], [None, None, None]], [['blue', 'blue', None], [None, None, None]]]

Example 2

Input: canvas_history(2, 2, ['draw', 'erase', 'undo', 'redo'], [[1, 1, 7], [1, 1], [], []])

Output: [[[None, None], [None, 7]], [[None, None], [None, None]], [[None, None], [None, 7]], [[None, None], [None, None]]]

Hints

Hint 1

Keep two stacks of whole canvases: one for undo, one for redo. Think about which stack each command pushes onto and which it pops from.

Hint 2

Flood fill can be done with an explicit stack instead of recursion. Skip any popped cell that no longer holds the original color, and do nothing at all if the original color already equals the new color.

Hint 3

Every draw, erase and fill must push to the undo history and clear the redo history, even if it changes nothing. Also copy rows when you save or output a canvas so later edits do not corrupt stored states.