Implement an ASCII canvas that fills rectangles clipped to the canvas bounds

Quick Overview

Implement an ASCII canvas class that fills inclusive rectangles with a character and returns the grid as a list of row strings. The task tests handling of reversed corners, clipping rectangles that extend past the canvas edges, and careful inclusive bounds in a small object-oriented design.

Implement an ASCII canvas that fills rectangles clipped to the canvas bounds

Company: Asana

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design an ASCII canvas that supports filling rectangular regions with characters. The canvas is a 2D grid of dimensions `width x height` initialized with space characters (`' '`). The `draw_rectangle` method fills every cell in the **inclusive** rectangle from `(x1, y1)` to `(x2, y2)` with the given character. Coordinates use `(0, 0)` as the top-left corner, where `x` is the column and `y` is the row. If the rectangle extends beyond the canvas bounds, only the in-bounds portion is drawn. If `x1 > x2` or `y1 > y2`, the coordinates should be normalized (treated as opposite corners). Implement the following methods: - `Canvas(width: int, height: int)`: create a canvas of that size filled with spaces. - `draw_rectangle(char: str, x1: int, y1: int, x2: int, y2: int)`: fill the rectangular area from `(x1, y1)` to `(x2, y2)` inclusive with the given character. Coordinates are normalized and clipped to canvas bounds. - `get_canvas() -> List[str]`: return the current canvas as a list of strings, one per row (top to bottom). Example 1: ```text Input: ["Canvas", "draw_rectangle", "draw_rectangle", "get_canvas"] [[5, 4], ["#", 0, 0, 2, 1], ["*", 2, 2, 4, 3], []] Output: [null, null, null, ["### ", "### ", " ***", " ***"]] canvas = Canvas(5, 4) # 5 wide, 4 tall, all spaces canvas.draw_rectangle('#', 0, 0, 2, 1) # fills the top-left 3x2 block with '#' canvas.draw_rectangle('*', 2, 2, 4, 3) # fills the bottom-right 3x2 block with '*' canvas.get_canvas() # ['### ', '### ', ' ***', ' ***'] ``` Example 2: ```text Input: ["Canvas", "draw_rectangle", "get_canvas"] [[3, 3], ["X", -1, -1, 10, 10], []] Output: [null, null, ["XXX", "XXX", "XXX"]] canvas = Canvas(3, 3) # 3x3 grid of spaces canvas.draw_rectangle('X', -1, -1, 10, 10) # rectangle clipped to the full canvas canvas.get_canvas() # ['XXX', 'XXX', 'XXX'] ``` ```hint Trace the bounds logic first Before writing the fill loops, trace your normalize and clip steps on two cases: corners given in reverse order, and a rectangle that lies entirely to the right of the canvas. The second case should leave the canvas unchanged. ``` ### Constraints & Assumptions - `1 <= width, height <= 1000` - `-10⁴ <= x1, y1, x2, y2 <= 10⁴` - `char` is a single printable ASCII character. - At most `10⁴` calls to `draw_rectangle`. ### What a Strong Answer Covers - A grid representation that makes writing one cell a constant-time operation, and a `get_canvas` that does not hand internal state to the caller. - Corner normalization and clipping done in an order that handles both reversed corners and rectangles lying fully off the canvas. - Correct inclusive bounds in the fill, with no off-by-one at the right and bottom edges. - The cost of each method, stated in terms of the clipped area and the canvas size. - Edge cases checked: a single cell, overlapping draws, partial clipping and full clipping. ### Follow-up Questions - How would you draw only the border of a rectangle, and which cells count as border when the rectangle is clipped? - How would you add an `undo` that reverts the most recent `draw_rectangle` call? - If draws far outnumber reads and most of them cover large areas, how could you avoid writing every cell on every draw? - How would the design change if rectangles had to be moved or deleted after they were drawn?

Overview: Implement an ASCII canvas class that fills inclusive rectangles with a character and returns the grid as a list of row strings. The task tests handling of reversed corners, clipping rectangles that extend past the canvas edges, and careful inclusive bounds in a small object-oriented design.

Asana logo
Asana
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an ASCII canvas that supports filling rectangular regions with characters.

The canvas is a 2D grid of dimensions width x height initialized with space characters (' '). The draw_rectangle method fills every cell in the inclusive rectangle from (x1, y1) to (x2, y2) with the given character. Coordinates use (0, 0) as the top-left corner, where x is the column and y is the row.

If the rectangle extends beyond the canvas bounds, only the in-bounds portion is drawn. If x1 > x2 or y1 > y2, the coordinates should be normalized (treated as opposite corners).

Implement the following methods:

  • Canvas(width: int, height: int) : create a canvas of that size filled with spaces.
  • draw_rectangle(char: str, x1: int, y1: int, x2: int, y2: int) : fill the rectangular area from (x1, y1) to (x2, y2) inclusive with the given character. Coordinates are normalized and clipped to canvas bounds.
  • get_canvas() -> List[str] : return the current canvas as a list of strings, one per row (top to bottom).

Example 1:

Input:
["Canvas", "draw_rectangle",  "draw_rectangle",  "get_canvas"]
[[5, 4],   ["#", 0, 0, 2, 1], ["*", 2, 2, 4, 3], []]

Output: [null, null, null, ["###  ", "###  ", "  ***", "  ***"]]

canvas = Canvas(5, 4)                       # 5 wide, 4 tall, all spaces
canvas.draw_rectangle('#', 0, 0, 2, 1)      # fills the top-left 3x2 block with '#'
canvas.draw_rectangle('*', 2, 2, 4, 3)      # fills the bottom-right 3x2 block with '*'
canvas.get_canvas()                         # ['###  ', '###  ', '  ***', '  ***']

Example 2:

Input:
["Canvas", "draw_rectangle",      "get_canvas"]
[[3, 3],   ["X", -1, -1, 10, 10], []]

Output: [null, null, ["XXX", "XXX", "XXX"]]

canvas = Canvas(3, 3)                       # 3x3 grid of spaces
canvas.draw_rectangle('X', -1, -1, 10, 10)  # rectangle clipped to the full canvas
canvas.get_canvas()                         # ['XXX', 'XXX', 'XXX']

Constraints & Assumptions

  • 1 <= width, height <= 1000
  • -10⁴ <= x1, y1, x2, y2 <= 10⁴
  • char is a single printable ASCII character.
  • At most 10⁴ calls to draw_rectangle .

What a Strong Answer Covers Guidance

  • A grid representation that makes writing one cell a constant-time operation, and a get_canvas that does not hand internal state to the caller.
  • Corner normalization and clipping done in an order that handles both reversed corners and rectangles lying fully off the canvas.
  • Correct inclusive bounds in the fill, with no off-by-one at the right and bottom edges.
  • The cost of each method, stated in terms of the clipped area and the canvas size.
  • Edge cases checked: a single cell, overlapping draws, partial clipping and full clipping.

Follow-up Questions Guidance

  • How would you draw only the border of a rectangle, and which cells count as border when the rectangle is clipped?
  • How would you add an undo that reverts the most recent draw_rectangle call?
  • If draws far outnumber reads and most of them cover large areas, how could you avoid writing every cell on every draw?
  • How would the design change if rectangles had to be moved or deleted after they were drawn?