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 ofsizeBytesfor every successfully processed artifact identifier exactly once. It returns0before 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
artifactIdreturnsfalseand leaves the accepted identifiers, global storage, collection existence, and every collection total unchanged. - Top selection.
topCollections(limit)returns the firstmin(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.
| Signature | Returns | Behavior |
|---|---|---|
ArtifactCollectionRanker() | Not applicable | Creates an empty ranker with no accepted artifacts or collections. |
processArtifact(artifactId: string, sizeBytes: integer, collectionIds: string[]) | boolean | Returns 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() | integer | Returns the current global byte total. |
topCollections(limit: integer) | CollectionStat[] | Returns the ranked collection statistics. It returns an empty list when no collection exists. |
Examples
| Step | Operation | Result |
|---|---|---|
| 1 | processArtifact("shared.bin", 40, ["alpha", "beta"]) | true |
| 2 | processArtifact("alpha-only.bin", 10, ["alpha"]) | true |
| 3 | processArtifact("beta-only.bin", 10, ["beta"]) | true |
| 4 | totalStorage() | 60 |
| 5 | topCollections(5) | [{"collectionId":"alpha","totalSizeBytes":50},{"collectionId":"beta","totalSizeBytes":50}] |
| 6 | processArtifact("shared.bin", 999, ["gamma"]) | false |
| 7 | topCollections(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,000calls toprocessArtifact,20,000accepted artifacts,20,000collections, and20,000report 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.