KatInterview

Simulate Stacked Rectangles on a Small Text Canvas

Data Structure DesignAuto-graded · 23 test cases
MediumAuto-gradedData Structure Designsimulationordered-dictdesign

Last updated

Simulate add, remove, reorder and render commands on a layered character canvas, resolving overlaps by layer order and clipping shapes at the edges.

You are given a text canvas with canvas_width columns and canvas_height rows, and a list of commands to run against it in order. The canvas holds filled rectangles stacked in layers. Each rectangle is drawn with one character, and a rectangle on a higher layer hides whatever is beneath it.

Coordinates are (x, y): x is the column (0 at the left) and y is the row (0 at the top). A rectangle with top-left corner (x, y), width w and height h covers the cells with x <= column < x + w and y <= row < y + h.

Commands

Command i is named by commands[i] and takes the argument list command_args[i]:

CommandArgumentsEffectResult
"add_rectangle"[shape_id, x, y, w, h, fill]Places a new rectangle on the top layer.True if added, otherwise False.
"remove"[shape_id]Deletes the rectangle with that id.True if it existed, otherwise False.
"bring_to_front"[shape_id]Moves the rectangle to the top layer.True if it existed, otherwise False.
"send_to_back"[shape_id]Moves the rectangle to the bottom layer.True if it existed, otherwise False.
"render"[]Draws the current canvas.A list of canvas_height strings, each of length canvas_width.

Rules

  • add_rectangle returns False and changes nothing if shape_id is already in use, or if w <= 0 or h <= 0. A removed id may be used again, and the new rectangle goes on the top layer.
  • Rectangles may extend beyond the canvas, in whole or in part, and they may have negative coordinates. They are accepted and stored as given. Only the cells inside the canvas are drawn, so a rectangle entirely outside the canvas is valid but invisible.
  • Commands that fail (unknown id, duplicate id, bad size) leave the state unchanged.
  • Reordering a rectangle that is already in the requested position still returns True.
  • When rendering, every cell not covered by any rectangle shows '.'. A covered cell shows the fill character of the highest-layer rectangle covering it.
  • Moving a rectangle to another layer changes only the layer order. It does not change the rectangle's position or size.

Task

Implement the function so that it runs all commands in order and returns a list of results, one entry per command, in the same order as the commands.

Constraints

  • 1 <= canvas_width, canvas_height <= 100
  • len(commands) == len(command_args), and every command is one of the five names above.
  • shape_id is a string and fill is a single character.
  • x, y, w, h are integers. Coordinates may be negative or larger than the canvas, and w and h may be zero or negative (in which case adding fails).

Examples

Example 1

Input: run_canvas_commands(10, 6, ['add_rectangle', 'add_rectangle', 'render', 'bring_to_front', 'render'], [['A', 1, 1, 4, 3, 'A'], ['B', 3, 2, 4, 3, 'B'], [], ['A'], []])

Output: [True, True, ['..........', '.AAAA.....', '.AABBBB...', '.AABBBB...', '...BBBB...', '..........'], True, ['..........', '.AAAA.....', '.AAAABB...', '.AAAABB...', '...BBBB...', '..........']]

Example 2

Input: run_canvas_commands(10, 6, ['render', 'add_rectangle', 'render', 'remove', 'render'], [[], ['x', -2, 4, 5, 4, '#'], [], ['missing'], []])

Output: [['..........', '..........', '..........', '..........', '..........', '..........'], True, ['..........', '..........', '..........', '..........', '###.......', '###.......'], False, ['..........', '..........', '..........', '..........', '###.......', '###.......']]

Example 3

Input: run_canvas_commands(5, 4, ['add_rectangle', 'add_rectangle', 'send_to_back', 'render'], [['a', 0, 0, 5, 4, 'a'], ['b', 1, 1, 2, 2, 'b'], ['b'], []])

Output: [True, True, True, ['aaaaa', 'aaaaa', 'aaaaa', 'aaaaa']]

Hints

Hint 1

Keep the shapes in a structure that remembers layer order from bottom to top and still lets you look up a shape by id. Python's OrderedDict has move_to_end for exactly this.

Hint 2

You do not need to maintain a pixel grid while commands run. Store the shapes, and only build the grid when a render command arrives.

Hint 3

To render, start with a grid of '.', then paint shapes from the bottom layer to the top so later shapes overwrite earlier ones. Clip each rectangle's column and row ranges to the canvas so out-of-bounds shapes need no special handling.