Document Layers with Undo

Asked at Figma· Screen

Reported by the problem's creator, not verified

Import another question

A design document is a set of layers. Each layer has an integer id and holds named properties such as color or width, each with a string value.

A layer exists only while it has at least one property. Reading a layer that has none returns None, whether it was never touched or all of its properties were undone.

You build the Document class behind an editor's undo history in three stages. Each example drives one Document instance through a list of [method, [args]] calls and lists every call's return value.

Examples

  1. Example 1

    Input: init = [], operations = [["apply",[1,"color","green"]],["apply",[2,"shape","triangle"]],["apply",[1,"color","blue"]],["get_layer",[1]],["undo",[]],["get_layer",[1]],["get_layer",[2]],["undo",[]],["undo",[]],["get_layer",[2]],["get_layer",[1]],["undo",[]]]

    Output: [null,null,null,[["color","blue"]],true,[["color","green"]],[["shape","triangle"]],true,true,null,null,false]

    Explanation: The third apply overwrites layer 1's color, so the first undo restores green. The next two undos remove layer 2's shape and layer 1's color, leaving both layers without properties, so reading them returns None. The last undo has nothing left to revert and returns False.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.