Layered ASCII Canvas Command Processor
Updated
Problem statement
Given commands, run them in order on a canvas with width 10 and height 6, and return the printed snapshots. Coordinates start at zero: x grows from left to right and y grows from top to bottom. The background character is ..
The commands are:
DRAW_RECTANGLE left top right bottom ch: draw a filled rectangle, edges included, using the single uppercase characterch. A new rectangle goes in front of all others.ERASE_AREA left top right bottom: erase for good every rectangle cell that currently lies inside this area, edges included. Hidden cells are erased too. A rectangle keeps its erased cells as holes that move with it when it is dragged later.DRAG_AND_DROP fromX fromY toX toY: pick the frontmost rectangle that has a non-erased cell at the source point. Shift that rectangle so the picked cell lands on the destination point. It keeps its layer position. If no rectangle shows at the source, do nothing.BRING_TO_FRONT x y: move the frontmost rectangle that has a non-erased cell at that point in front of all others. If no rectangle shows there, do nothing.PRINT_CANVAS: add one snapshot of the current canvas to the answer.
A snapshot joins the six rows, top to bottom, with /. Each cell shows the character of the frontmost rectangle whose cell there is not erased, or . if there is none. Return the snapshots in PRINT_CANVAS order.
Function
processCanvas(commands: String[]) → String[]Examples
Example 1
commands = ["DRAW_RECTANGLE 1 1 4 3 A","PRINT_CANVAS","DRAW_RECTANGLE 3 0 6 2 B","PRINT_CANVAS","ERASE_AREA 4 1 5 1","PRINT_CANVAS"]return = ["........../.AAAA...../.AAAA...../.AAAA...../........../..........","...BBBB.../.AABBBB.../.AABBBB.../.AAAA...../........../..........","...BBBB.../.AAB..B.../.AABBBB.../.AAAA...../........../.........."]Rectangle B is drawn over part of A. Erasing columns 4 through 5 on row 1 removes the B cells there and the hidden A cell below B.
Example 2
commands = ["DRAW_RECTANGLE 1 1 2 2 X","DRAG_AND_DROP 1 1 5 3","PRINT_CANVAS","ERASE_AREA 5 3 5 3","DRAG_AND_DROP 6 4 7 5","PRINT_CANVAS"]return = ["........../........../........../.....XX.../.....XX.../..........","........../........../........../........../.......X../......XX.."]The first drag shifts X right by 4 and down by 2. After its top-left cell is erased, the second drag grabs its bottom-right cell and moves the shape, hole included.
Constraints
1 <= commands.length <= 1000.- Every command has exactly the syntax shown above and integer coordinates within the canvas.
- Every drawn rectangle satisfies
0 <= left <= right < 10and0 <= top <= bottom < 6. - Every drag that selects a rectangle keeps that rectangle fully inside the canvas after translation.
- Every
chis one uppercase English letter. - At most
200rectangles are created.
More Asana Onsite Interview questions
Similar questions at other companies
- Ultimate Tic-Tac-Toe Move SimulatorHebbia · PHONE SCREEN
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- Randomized Collection with Duplicate ValuesOkta · ONSITE INTERVIEWOkta interview questions
- Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Hybrid Run-Length and Bit-Packing EncoderDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Transformer Attention and SwiGLU Forward PassGoogle · ONSITE INTERVIEWGoogle interview questions
- In-Memory SQL with CSV InitializationMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Spiral MatrixAthenahealth · ONSITE INTERVIEW
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions