FastPrepLayer History with Batch Undo and Redo

Layer History with Batch Undo and Redo

Figma logoFigma● MediumFULLTIMEINTERNOAPHONE SCREEN
Learn

Problem statement

A design document contains layers. Each layer has an integer ID and a dictionary of string properties.

Process a finite sequence of history operations:

  • ["apply", layerId, key1, value1, ...] updates one or more properties on one layer. Outside an open batch, that apply is immediately committed as one undoable change.
  • ["beginBatch"] starts a batch. Every following apply takes effect immediately but belongs to that batch.
  • ["commitBatch"] commits the non-empty open batch as one undoable change.
  • ["undo"] reverses the most recent committed change.
  • ["redo"] reapplies the most recently undone change.

A new committed apply or batch clears the redo history. An undo or redo with no available history does nothing.

Return the final layers ordered by increasing layer ID. Serialize each layer as id{key=value,...}, with property keys in lexicographic order. Omit a layer when it 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}"]

The first undo restores layer 1 to green. The second undo removes the entire apply that created layer 2.

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 last standalone apply is one commit, so undo restores the state produced by the earlier batch.

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}"]

Redo reapplies the whole batch, including the last value written to color.

Constraints

  • 1 <= operations.length <= 200000.
  • At most 200000 property assignments appear across all apply operations.
  • Layer IDs are positive 32-bit integers written in canonical decimal form.
  • Keys and values are non-empty strings without {, }, =, ,, or :.
  • Batches do not nest, every commitBatch closes a non-empty open batch, and the sequence ends with no open batch.

More Figma problems

See Figma hiring insights
public String[] processLayerHistory(String[][] operations) {
    // Write your code here.
}
operations[["apply","1","color","green"],["apply","2","shape","triangle","color","blue"],["apply","1","color","pink"],["undo"],["undo"]]
expected["1{color=green}"]
Checking account…