Problemrecoverable soft delete registry

Session: Sign in to solve

Solution.txt

Recoverable Soft-Delete Registry

Problem

Design RecoverableSoftDeleteRegistry to manage named collections that can be deleted, viewed, and restored during a fixed retention window.

Requirements

  • Initialization. Construction stores every supplied collection ID and name as live and uses the supplied retention interval for every later deletion.
  • Deletion. Deleting a live collection returns true and creates a tombstone with deletedAt = nowSeconds and expiresAt = nowSeconds + retentionSeconds. Deleting an absent or already-deleted collection returns false without changing state or timestamps.
  • Restoration. Restoring a tombstone while nowSeconds < expiresAt returns true, makes the original collection live, and preserves its ID and name. Restoring an absent ID, a live collection, or a tombstone at or after expiry returns false without changing registry state.
  • Deleted view. getDeletedCollections(nowSeconds) returns exactly the ID-name mappings for tombstones with nowSeconds < expiresAt. Map iteration order is not observable.

API

SignatureReturnsBehavior
RecoverableSoftDeleteRegistry(initialCollections: map<string, string>, retentionSeconds: integer)Not applicableCopies the supplied collections into live state and fixes the retention interval.
deleteCollection(collectionId: string, nowSeconds: integer)booleanApplies the deletion transition or returns false atomically.
restoreCollection(collectionId: string, nowSeconds: integer)booleanRestores one unexpired tombstone or returns false atomically.
getDeletedCollections(nowSeconds: integer)map<string, string>Returns all and only currently recoverable tombstones.

Examples

For RecoverableSoftDeleteRegistry({"c1": "Payments", "c2": "Health"}, 10):

StepOperationResult
1deleteCollection("c1", 100)true
2getDeletedCollections(105){"c1": "Payments"}
3restoreCollection("c1", 109)true
4getDeletedCollections(109){}
5deleteCollection("c1", 120)true
6restoreCollection("c1", 130)false

Constraints

  • 0 <= initialCollections.size <= 10,000.
  • IDs and names are nonempty; IDs are unique map keys and names may repeat.
  • 1 <= retentionSeconds <= 1,000,000,000.
  • 0 <= nowSeconds <= 1,000,000,000,000.
  • Time-bearing calls on one instance use nondecreasing nowSeconds.
  • At most 15,000 public method calls occur per testcase.
  • Expiry arithmetic uses signed 64-bit integers.

Notes

A collection is either live or represented by one tombstone. Calls are sequential, and concurrent use has no guarantee.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.