Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Batch Commit and Batch Undo

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Batch Commit and Batch Undo. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Figma groups several related FigJam or Figma Design canvas edits into one history entry so a single undo removes the entire user action. Implement batch_commit and batch_undo for a simplified document model.

A document is a dictionary mapping object IDs to integer values. Each change is represented as [object_id, before, after]. A batch commit must apply every change and store the entire batch as one undo entry. A batch undo must restore the most recent batch in reverse order. If there is no committed batch, undo does nothing.

Implement process_operations, which uses these operations:

  • {"type": "commit", "changes": [[id, before, after], ...]}
  • {"type": "undo"}

Return the final document dictionary. The input document should not be mutated by the caller.

Formal Specification

Input: initial_document, a dictionary from strings to integers, and actions, a list of commit or undo dictionaries.

Output: A dictionary containing the document state after all actions.

Constraints

  • 0 <= len(actions) <= 10^5
  • Each action contains at most 10^4 changes
  • Each object appears at most once within a committed batch
  • Every before value matches the current document value
  • Object IDs are nonempty strings and values are integers

Function Signature

def process_operations(initial_document, actions):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output