Problemoverlapping artifact collection ranker

Session: Sign in to solve

Solution.txt

Overlapping Artifact Collection Ranker

Problem

Design ArtifactCollectionRanker to process uniquely identified artifacts and report both global storage and the largest flat collections. One artifact may belong to several collections, and calls are sequential.

Requirements

  • Global storage. totalStorage() returns the sum of sizeBytes for every successfully processed artifact identifier exactly once. It returns 0 before any artifact is accepted.
  • Overlapping collections. An accepted artifact contributes its size once to every collection named in collectionIds. The list may be empty and may contain several distinct collection identifiers.
  • Duplicate rejection. Processing an already accepted artifactId returns false and leaves the accepted identifiers, global storage, collection existence, and every collection total unchanged.
  • Top selection. topCollections(limit) returns the first min(limit, collectionCount) collection statistics ranked by descending current total size. Each statistic contains the collection identifier and total size.
  • Deterministic ties. Collections with equal total size are ordered by ascending ASCII collection identifier.

API

CollectionStat is a value with collectionId: string and totalSizeBytes: integer.

SignatureReturnsBehavior
ArtifactCollectionRanker()Not applicableCreates an empty ranker with no accepted artifacts or collections.
processArtifact(artifactId: string, sizeBytes: integer, collectionIds: string[])booleanReturns true and accepts an unseen artifact, adding its size once to global storage and once to each named collection. Returns false without changing state for an accepted identifier.
totalStorage()integerReturns the current global byte total.
topCollections(limit: integer)CollectionStat[]Returns the ranked collection statistics. It returns an empty list when no collection exists.

Examples

StepOperationResult
1processArtifact("shared.bin", 40, ["alpha", "beta"])true
2processArtifact("alpha-only.bin", 10, ["alpha"])true
3processArtifact("beta-only.bin", 10, ["beta"])true
4totalStorage()60
5topCollections(5)[{"collectionId":"alpha","totalSizeBytes":50},{"collectionId":"beta","totalSizeBytes":50}]
6processArtifact("shared.bin", 999, ["gamma"])false
7topCollections(5)Unchanged; gamma does not exist.

Constraints

  • 1 <= artifactId.length <= 64.
  • Artifact and collection identifiers contain only ASCII letters, digits, ., _, and -.
  • 0 <= sizeBytes <= 10^12.
  • 0 <= collectionIds.length <= 20, and the identifiers in one call are nonempty and distinct.
  • 1 <= limit <= 1000.
  • At most 50,000 calls to processArtifact, 20,000 accepted artifacts, 20,000 collections, and 20,000 report calls.
  • All totals fit in a signed 64-bit integer.

Notes

Calls are sequential. Concurrent access and malformed or out-of-constraint input are outside the contract. Collections are flat; nested collections and membership changes after acceptance are not supported.

PRIVATE WORKSPACE

Checking your session…

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