Keeping snapshot memo tables separate

An editor creates morning and afternoon snapshots of two source records, a and b. Their detail fields initially refer to the same mutable object, as shown below. The source records and their detail object are all mutable.


Two source records refer to the same mutable detail object.


The function copyGraph(x, memo) makes a graph-preserving deep copy. A memo table maps each source object to its replacement object. If the table already contains a replacement for an object, the function returns that replacement immediately, without rereading or updating its fields. Otherwise, the function creates and records a replacement and recursively copies its fields using that same table.


Two distinct, initially empty tables, m1 and m2, are available. Each snapshot must preserve the shared detail relationship between its two copied records. The morning and afternoon snapshots must share no mutable objects with each other or with the source. The afternoon snapshot must include edits made to the source after the morning snapshot.


Complete the plan using only m1 or m2 in each blank. Tables are never cleared, and the calls run in the order described.

  1. Create the morning copy of a by calling copyGraph(a, m1).
  2. Create the morning copy of b by passing as the memo argument to copyGraph.
  3. Change a field inside the source detail object, without changing any source references.
  4. Create the afternoon copy of a by passing as the memo argument to copyGraph.
  5. Create the afternoon copy of b by passing as the memo argument to copyGraph.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Shallow vs Deep Copies Questions: