← Questions

Documents and layers: create, rename, move and remove

0
FigmaCodingLast asked Sep 2020

A coding interview question asked in Figma's coding round, last asked Sep 2020. It tests hash maps and simulation.

Process a list of operations on documents and the layers inside them, then return the final state of every document. Layer IDs are unique across all documents.

Each operation is a list of strings:

  • ["createDocument", doc_id] creates an empty document.
  • ["addLayer", doc_id, layer_id, value] adds a layer with a value to a document.
  • ["renameLayer", layer_id, new_value] changes a layer's value.
  • ["moveLayer", layer_id, target_doc_id] moves a layer to another document, keeping its ID and value.
  • ["removeLayer", layer_id] deletes a layer.

Return every document, ordered by document ID, serialized as doc_id{layer_id=value,...} with its layers ordered by layer ID. An empty document is doc_id{}.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • process(operations: list[list[str]]) -> list[str]

Example

[["createDocument","draft"],
 ["addLayer","draft","title","hello"],
 ["renameLayer","title","welcome"]]
-> ["draft{title=welcome}"]

[["createDocument","a"], ["createDocument","b"],
 ["addLayer","a","z","green"], ["addLayer","a","m","blue"],
 ["moveLayer","z","b"]]
-> ["a{m=blue}", "b{z=green}"]

[["createDocument","doc"], ["addLayer","doc","x","one"], ["removeLayer","x"]]
-> ["doc{}"]     # the document stays after its last layer is removed

Constraints

  • Up to 200,000 operations; at most 100,000 documents and 200,000 layers are created.
  • Every ID is unique when it is created, and every other operation refers to a document or layer that exists.
  • IDs and values are non-empty lowercase ASCII strings of at most 40 characters, and contain none of {, }, =, ,.

Clarifying questions

  • Can a removed layer's ID be reused by a later addLayer?
  • What should happen on an invalid operation, such as moving a layer to a document that does not exist? (Here it cannot happen; say what you would do.)
  • Is ordering by ID plain string comparison?
  • Is the final state all that is needed, or will callers query it between operations?

What a strong answer covers

  • A map from document ID to its layers (layer_id -> value) and a second map from layer ID to its document, so rename, move and remove are O(1) and never scan.
  • Keeping the two maps in step on every move and remove.
  • Sorting only once at the end: O(D log D + L log L) for D documents and L layers.
  • Building output strings with a join rather than repeated concatenation.
  • Tests: moving a layer into the document it is already in, removing the only layer, documents with no layers, many documents.

Follow-ups

  • Add an undo operation that reverts the last change.
  • Layers now have an order inside a document (a z-order), and moveLayer takes a position. What changes?
  • Support querying a document's state between operations without sorting it each time.

Where it's asked

  • Figma · Coding · Last asked Sep 202060 min · One or two pair-programming rounds against provided tests

What it tests

hash maps · simulation

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding