AlgoMaster Logo
AlgoMasterDesign Document Version Historyhard

Design Document Version History

hard

A document is built one line at a time. Any point in its life can be saved under a name, and the document can be returned to any saved name afterwards.

The starter code provides DocumentEditor, the coordinator used by the tests. Implement only its three Memento participants:

  • DocumentMemento, the memento that owns a saved copy of the lines.
  • TextDocument, the originator that creates and restores mementos.
  • VersionHistory, the caretaker that stores named mementos without reading them.

The provided coordinator behaves as follows:

  • DocumentEditor() starts with an empty document and no saved versions.
  • boolean write(String line) appends a line and returns true, or returns false once the document holds 10 lines.
  • String content() returns the lines joined by "|", or "EMPTY" when the document has no lines.
  • int lineCount() returns how many lines the document holds.
  • boolean saveVersion(String name) saves the current content under that name and returns true. A name already in use changes nothing and returns false.
  • boolean restore(String name) replaces the content with what that version holds and returns true. An unknown name changes nothing and returns false.
  • String versions() returns the saved names joined by commas in save order, or "NONE" when nothing is saved.
  • int versionCount() returns how many versions the history holds.

The history holds at most 4 versions. Saving a fifth drops the oldest, and the dropped name stops being restorable from that point on.

Restoring leaves the version in place. The same name can be restored any number of times, and writing after a restore never changes what that version holds.

Do not rewrite DocumentEditor. Make the three participants satisfy the API it already uses, and keep saved versions opaque to VersionHistory.

Example 1:

Input:

Output:

Explanation: Two lines written, then saved under one name. The content joins the lines with a bar, and the history reports the single version.

Example 2:

Input:

Output:

Explanation: Version v1 holds one line. Writing more and restoring v1 returns the document to that one line, and because restoring leaves v1 in place, the second restore gives the same result.

Constraints

  • 1 <= line.length <= 40
  • 1 <= name.length <= 20
  • The document holds at most 10 lines.
  • The history holds at most 4 versions.
  • At most 100 calls in total are made across all methods.

Starter Code

DocumentEditor is complete in every language below. Implement only DocumentMemento, TextDocument, and VersionHistory.

How the design is graded

needs 7/10 to pass
  • Snapshots are independent

    Full marks when a version copies the lines as it is built and copies them again as it is applied, so writing after a save never changes the version and writing after a restore never changes it either. Lose points heavily when the version keeps the document's own list, or when restoring hands that list back to the document.

  • The caretaker cannot read a version

    Full marks when the history stores named snapshot objects and returns them untouched, leaving the document as the only type that reads or writes the lines inside one. Lose points when the history holds raw line lists, or when it builds the content string itself.

  • Structure and naming

    Full marks when `VersionHistory` refuses repeated names, keeps at most four versions by dropping the oldest, returns `-1` for an unknown name, and lists names in save order, while `TextDocument` enforces ten lines and renders empty content as `EMPTY`. Lose points for exposing raw line lists through the caretaker or printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new DocumentEditor()null
write("# My Document")true
write("Introduction paragraph.")true
content()"# My Document|Introduction paragraph."
saveVersion("v1-intro")true
versions()"v1-intro"
versionCount()1

Two lines written, then saved under one name. The content joins the lines with a bar, and the history reports the single version.

Run checks these cases. Submit also runs a larger hidden set.