Sparse Canvas Paint History
Updated
Problem statement
Maintain a sparse two-dimensional canvas while processing operations in order.
[1, x, y, color]paints coordinate(x, y).[2, x, y]reads that coordinate and appends its current color to the result.[3]undoes the most recent paint that has not already been undone.[4]redoes the most recently undone paint.
An unpainted coordinate has color 0. Each paint is one history entry, including a paint that writes the current color. A new paint clears the redo history. Undo or redo does nothing when its corresponding history is empty.
Return all colors produced by read operations, in operation order.
Function
processCanvas(operations: int[][]) → int[]Examples
Example 1
operations = [[2,1,1],[1,1,1,5],[2,1,1],[3],[2,1,1],[4],[2,1,1]]return = [0,5,0,5]The first read sees the default color. Painting sets the color to 5; undo removes it, and redo restores it.
Example 2
operations = [[1,2,3,7],[1,2,3,9],[2,2,3],[3],[2,2,3],[3],[2,2,3]]return = [9,7,0]Successive undos reveal the earlier color and then the unpainted state.
Example 3
operations = [[1,0,0,1],[3],[1,1,1,2],[4],[2,0,0],[2,1,1]]return = [0,2]The second paint clears redo history, so the later redo cannot restore the first coordinate.
Constraints
1 <= operations.length <= 200000.-10^9 <= x, y <= 10^9.1 <= color <= 10^9for every paint.- Every operation has exactly the fields specified by its operation code.
More Figma Phone Screen questions
- Recursive Display SortPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Route Graph Reachability with a Broken LinkPHONE SCREEN · Seen Oct 2026
- Rich Text Parser Part 3 (Depth Tokens)PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Rich Text Parser Part 2 (Parse Nested Groups)PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Rich Text Parser Part 1 (Validate Tokens)PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Layer History with Batch Undo and RedoOA · PHONE SCREEN · Seen Sep 2026
- Table Selection and Color UpdatesPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Topmost Accessible NodesPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2025
- Intersection of Two RectanglesPHONE SCREEN · Seen Aug 2024
- Shell Path AutocompletePHONE SCREEN · ONSITE INTERVIEW · Seen May 2022
Similar questions at other companies
- Key-Value Store with Nested TransactionsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Nested Recipe Cart TransactionsDecagon · PHONE SCREENDecagon interview questions
- Transactional In-Memory RedisHashiCorp · ONSITE INTERVIEWHashiCorp interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · 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
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- File Storage ServiceCresta · PHONE SCREENCresta interview questions