Draw Overlapping ASCII Rectangles
Updated
Problem statement
Draw a sequence of ASCII rectangles on a finite canvas. The canvas has canvasWidth columns and canvasHeight rows and is initially filled with the one-character string background. Coordinates use a top-left origin: x increases to the right and y increases downward.
Each rectangles[i] is [x, y, width, height]. Its boundary uses borderChars[i], while cells strictly inside the boundary use fillChars[i]. A rectangle whose width or height is 1 consists entirely of boundary cells.
Draw rectangles in input order. A later rectangle overwrites every earlier character at each canvas cell it covers. Drawing is clipped to the canvas: cells of a rectangle that fall outside the canvas are skipped. Whether a visible cell is boundary or interior still depends on the full rectangle, before any clipping.
Return the final canvas as canvasHeight strings ordered from top row to bottom row, each containing exactly canvasWidth characters.
Function
drawRectangles(canvasWidth: int, canvasHeight: int, background: String, rectangles: int[][], borderChars: String[], fillChars: String[]) → String[]Examples
Example 1
canvasWidth = 6canvasHeight = 5background = "."rectangles = [[1,1,4,3]]borderChars = ["#"]fillChars = ["+"]return = ["......",".####.",".#++#.",".####.","......"]The rectangle covers columns 1 through 4 and rows 1 through 3. Its boundary cells use #. Only the middle row has cells strictly inside the boundary, and those inner cells use +. Every other cell keeps the background .. The final rows are ......, .####., .#++#., .####. and .......
Example 2
canvasWidth = 7canvasHeight = 5background = "."rectangles = [[1,1,5,3],[4,0,4,4]]borderChars = ["#","@"]fillChars = ["+","o"]return = ["....@@@",".###@oo",".#++@oo",".###@@@","......."]The first rectangle draws a # boundary with + inside. The second rectangle starts in the top row and is drawn last, so it overwrites the right end of the first rectangle. The second rectangle's rightmost column falls past the canvas edge and is clipped. Its visible cells still count as boundary or interior based on the full rectangle. So its left column shows @, while the last canvas column shows o in that rectangle's middle rows. The final rows are ....@@@, .###@oo, .#++@oo, .###@@@ and ........
Example 3
canvasWidth = 4canvasHeight = 3background = "_"rectangles = [[-2,-1,3,3],[2,1,1,2]]borderChars = ["X","|"]fillChars = ["o","!"]return = ["X___","X_|_","__|_"]Most of the first rectangle lies above and to the left of the canvas. Only its right boundary reaches the canvas, in the leftmost column of every row except the bottom one, so those cells show X. The second rectangle has width 1, so every cell it covers uses its boundary character |. All other cells keep the background _. The final rows are X___, X_|_ and __|_.
Constraints
1 <= canvasWidth, canvasHeightandcanvasWidth * canvasHeight <= 2 * 10^5.0 <= rectangles.length <= 10^4.- Each
rectangles[i]is[x, y, width, height], where-10^9 <= x, y <= 10^9and1 <= width, height <= 10^9. borderChars.length == fillChars.length == rectangles.length.background, everyborderChars[i], and everyfillChars[i]is a one-character printable ASCII string other than a line break.- For every rectangle,
borderChars[i] != fillChars[i]. - Rectangles are drawn in input order and clipped to the canvas.
More Amperity Phone Screen questions
Similar questions at other companies
- Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions
- Spiral Order with ObstaclesTemu · PHONE SCREENTemu interview questions
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Spiral Matrix TraversalByteDance · PHONE SCREENByteDance interview questions
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN
- Longest Consecutive Same-Color LineGoogle · PHONE SCREENGoogle interview questions
- Diagonal TraverseHive · PHONE SCREENHive interview questions
- Build a Bird’s-Eye-View Height GridNVIDIA · PHONE SCREENNVIDIA interview questions
- Multi-Channel Valid ConvolutionTesla · PHONE SCREENTesla interview questions
- Read a Matrix in Spiral OrderAdobe · PHONE SCREENAdobe interview questions