Layer History with Batch Undo and Redo
Updated
Problem statement
A design document has layers. Each layer has an integer ID and a dictionary of string properties, where each property is a key with a value.
You get a list of history operations. Run them in order:
["apply", layerId, key1, value1, ...]sets one or more properties on one layer. When no batch is open, this apply is committed right away, which means it is saved as one change you can undo.["beginBatch"]opens a batch. Each apply after it still takes effect at once, but it becomes part of that batch.["commitBatch"]commits the open batch as one change you can undo. The batch is never empty at this point.["undo"]reverses the most recent committed change.["redo"]applies again the change that was undone most recently.
When a new apply or batch is committed, the redo history is cleared. An undo or redo with nothing to undo or redo does nothing.
Return the final layers sorted by layer ID, smallest first. Write each layer as id{key=value,...}, with its keys in lexicographic order, which means sorted as strings, character by character. Leave out any layer that has no properties.
Function
processLayerHistory(operations: String[][]) → String[]Examples
Example 1
operations = [["apply","1","color","green"],["apply","2","shape","triangle","color","blue"],["apply","1","color","pink"],["undo"],["undo"]]return = ["1{color=green}"]Each apply here is its own change. The first undo reverses the last apply, so layer 1 goes back to green. The second undo reverses the whole apply that created layer 2, so that layer has no properties and is left out. Only the green layer remains.
Example 2
operations = [["apply","1","color","green"],["beginBatch"],["apply","2","shape","triangle","color","blue"],["apply","1","color","pink"],["commitBatch"],["apply","1","color","white"],["undo"]]return = ["1{color=pink}","2{color=blue,shape=triangle}"]The batch adds a second layer and turns the first layer pink. It is committed as one change. The last apply turns the first layer white outside any batch, so it is a change of its own. The undo reverses only that apply. The layers go back to how the batch left them: the first layer is pink, and the second has a blue color and a triangle shape.
Example 3
operations = [["apply","7","name","button"],["beginBatch"],["apply","7","color","red"],["apply","7","color","blue","text","Save"],["commitBatch"],["undo"],["redo"]]return = ["7{color=blue,name=button,text=Save}"]The batch sets color twice, first to red and then to blue, and adds a text property. The undo reverses the whole batch, which leaves only the name. The redo applies the whole batch again, so color ends as blue, its last value in the batch, and the text is back.
Constraints
1 <= operations.length <= 200000.- All apply operations together set at most
200000properties, counting each key-value pair once. - Layer IDs are positive 32-bit integers, written as plain decimal digits with no sign and no leading zeros.
- Keys and values are non-empty strings without
{,},=,,, or:. - Batches never nest. Every
commitBatchcloses an open batch that holds at least one apply. When the operations end, no batch is open.
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
- 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
- Sparse Canvas Paint HistoryPHONE SCREEN · Seen Sep 2023
- 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
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions