Layered Rectangle Canvas
Updated
Problem statement
You are given a rectangular character canvas with rows rows and cols columns. Every uncovered cell initially contains '.'.
Process each string in commands from left to right. Coordinates are zero-indexed, and every command has one of these forms:
DRAW id top left height width chcreates a filled rectangle with a unique identifierid. Its top-left cell is(top, left), it occupiesheightrows andwidthcolumns, and every occupied cell contributes the uppercase characterch. A newly drawn rectangle is placed above every rectangle that already exists.MOVE id rowDelta colDeltatranslates the existing rectangleidby addingrowDeltato its top coordinate andcolDeltato its left coordinate. Moving a rectangle does not change its layer position.
At any cell covered by multiple rectangles, the topmost rectangle contributes the visible character. When a rectangle moves away, any lower rectangle underneath it becomes visible again.
Return the final canvas as an array of rows strings, each of length cols.
Function
renderRectangleCanvas(rows: int, cols: int, commands: String[]) → String[]Examples
Example 1
rows = 4cols = 7commands = ["DRAW back 0 0 3 4 A","DRAW front 1 2 2 4 B"]return = ["AAAA...","AABBBB.","AABBBB.","......."]The rectangle front was drawn later, so its 'B' cells cover the overlapping portion of back.
Example 2
rows = 4cols = 6commands = ["DRAW low 0 0 2 3 L","DRAW high 1 1 2 3 H","MOVE low 1 2"]return = ["......",".HHHL.",".HHHL.","......"]Moving low preserves its original lower layer. Its cells become visible only where high does not cover them.
Example 3
rows = 5cols = 7commands = ["DRAW base 1 1 3 5 X","DRAW top 2 2 2 2 O","MOVE top -1 3"]return = [".......",".XXXXOO",".XXXXOO",".XXXXX.","......."]The upper rectangle moves to columns 5 and 6. Its old cells reveal base, while its new overlap at column 5 remains visible as 'O'.
Constraints
1 <= rows, cols <= 50.1 <= commands.length <= 1000.- At most
200rectangles are drawn. - Every
idis a nonempty alphanumeric string; eachDRAWuses a new ID, and eachMOVEnames an existing ID. - Each
chis one uppercase English letter. - Every rectangle has positive height and width, and every draw or move leaves the complete rectangle inside the canvas.
More Rippling Onsite Interview questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delivery Cost TrackerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Maximum Currency Conversion Along a Simple PathPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent WordsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Future-Effective Employee SalaryONSITE INTERVIEW · Seen Aug 2026
- Filter and Sort Scheduled TasksONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Configurable Tic-Tac-Toe GameEliseAI · PHONE SCREEN
- BitFont Part 2 - Render a WordStripe · ONSITE INTERVIEWStripe interview questions
- Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- First Completed Mingo LineEpic · OAEpic interview questions
- Hidden ArtifactsIMC Trading · OAIMC Trading interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- Mutable Points in a Hash Set and a ListMillennium · PHONE SCREENMillennium interview questions