AlgoMaster Logo
AlgoMasterRepair a Document Contracteasy

Repair a Document Contract

easy

Refactor a DocumentLibrary whose snapshot type claims to support editing.

Every document in the library can be read. The starter also requires every document to implement replace, so Snapshot rejects every replacement request. That silently strengthens the editing contract from “any text” to “no text” for one subtype.

  • addEditable(title, text) and addSnapshot(title, text) add a document and return its index.
  • replace(index, text) replaces all text and returns true only for an editable document. Each successful replacement increments its revision.
  • text(index) returns the stored text, or "UNKNOWN" for an invalid index.
  • revision(index) returns the revision, or -1 for an invalid index. Snapshots remain at revision 0.
  • describe(index) returns "Editable <title> r<revision>: <text>", "Snapshot <title>: <text>", or "UNKNOWN".

Keep snapshots under a readable contract and model editing as a separate capability.

  • Document promises only text(), revision(), and describe().
  • Editable adds replace(text) and accepts every string, including the empty string.
  • A successful replacement stores the complete supplied text and increments the revision exactly once.
  • A document without the editing capability is never asked to honor replace; the library returns false without mutation.
  • Invalid indexes return the documented sentinels and never throw.

This exercise focuses on capability applicability. Readability does not imply editability, so a snapshot must not inherit an editing precondition it can never satisfy.

Operations

Results

addEditable("Plan", "draft"), replace(0, "final"), revision(0)

0, true, 1

addSnapshot("Terms", "v1"), replace(0, "v2"), text(0)

0, false, "v1"

Replacing an editable document with "" is valid: it returns true, stores the empty string, and advances the revision.

  • Titles are non-empty strings; document text may be empty.
  • Indexes and revision counts fit in a signed 32-bit integer.
  • The number of successful revisions will not overflow.
  • Indexes may be invalid; return the specified sentinel values instead of throwing.

How the design is graded

needs 7/10 to pass
  • Capability contracts

    Full marks when Snapshot implements readable Document but not Editable. Lose points heavily when a snapshot remains a subtype of an editing contract it cannot honor.

  • Honest replacement

    Full marks when every Editable implementation changes the complete text and increments its revision before returning true, while non-editable entries return false without mutation. Lose points for silent no-ops, concrete-type checks, or exceptions.

  • Library behavior

    Full marks when the library stores both kinds through the readable contract, preserves insertion order, and returns exact invalid-index sentinels. Lose points for duplicated read logic or printed output.

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 DocumentLibrary()null
addEditable("Plan", "draft")0
replace(0, "final")true
text(0)"final"
revision(0)1
describe(0)"Editable Plan r1: final"

An editable document honors replacement and advances its revision.

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