Build a Progressive Cloud Storage System
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
# Build a Progressive Cloud Storage System
The source reports four storage stages but not the exact API. The command encoding, quota accounting, compression representation, and failure values below are practice assumptions that make the exercise executable.
Implement an in-memory service and this driver:
```python
def run_cloud_storage(operations: list[list[object]]) -> list[object]:
...
```
Paths are opaque absolute strings beginning with `/`; parent directories are implicit. File contents are Python `bytes`. The service starts with a reserved `admin` owner that has unlimited quota.
## Stage 1: Files
- `["ADD_FILE", path, content] -> bool` adds an uncompressed admin-owned file. Return `False` if `path` already exists.
- `["READ_FILE", path] -> bytes | None` returns the exact logical content whether the file is compressed or not, or `None` when absent.
- `["COPY_FILE", source, destination] -> bool` copies the complete stored representation to a new admin-owned path. Return `False` if the source is absent or the destination exists, including a self-copy.
## Stage 2: Search
- `["FIND", pattern] -> list[str]` performs a whole-path glob match. The only metacharacter is `*`, which matches any sequence of zero or more characters; every other character is literal. Return matching paths in lexicographic order.
## Stage 3: Users and Quotas
- `["ADD_USER", user_id, quota] -> bool` creates a non-admin user with a positive byte quota. Duplicate IDs and the reserved ID `admin` return `False`.
- `["SET_QUOTA", user_id, quota] -> bool` changes an existing non-admin user's positive quota. Return `False` if the user is absent or the new quota is below current charged usage.
- `["ADD_FILE_AS", user_id, path, content] -> bool` adds an uncompressed file owned by that user.
- `["COPY_FILE_AS", user_id, source, destination] -> bool` copies the source's logical content, compressed state, logical size, and charged size, but makes the caller the owner of the copy.
- `["GET_USAGE", user_id] -> list[int] | None` returns `[charged_bytes, quota]`, or `None` for an unknown user. For `admin`, return `[charged_bytes, -1]`, where `-1` denotes unlimited quota.
An uncompressed file's logical size and charged size are both `len(content)`. A user-owned add or copy returns `False` when the added charged size would exceed quota. Failed operations are atomic. There is no read-access-control rule in this practice contract; ownership controls quota charging and compression authorization only.
## Stage 4: Compression and Restoration
- `["COMPRESS", user_id, path, compressed_size] -> bool` succeeds only for an existing, uncompressed file owned by `user_id`. It keeps the logical content and logical size, marks the file compressed, and changes charged size to `compressed_size`.
- `["RESTORE", user_id, path] -> bool` succeeds only for an existing compressed file owned by `user_id`. It restores charged size to logical size and fails atomically if that increase would exceed quota.
`compressed_size` is supplied by the operation; no codec is required. It must satisfy `0 <= compressed_size <= logical_size`. Compression may therefore be size-neutral. Copying a compressed file preserves its compressed state. Compression and restoration by `admin` are allowed and use the same ownership rule.
## Errors and Constraints
- A missing user, ownership mismatch, duplicate path, absent source, invalid state transition, or quota failure returns the operation-specific `False` or `None` value above and changes nothing.
- A malformed command, unknown command, non-bytes content, non-absolute path, nonpositive quota, or out-of-range compressed size raises `ValueError` before mutation.
- Zero-byte files are valid. At most `100_000` operations are supplied.
Explain indexes for path lookup, owner usage, and search. Test glob boundaries, duplicate destinations, exact quota fills, compressed copies, size-neutral compression, restoration failure, and quota changes.
Quick Answer: Build a progressive in-memory cloud storage service with file operations, glob search, users, quotas, compression, and restoration. Handle ownership, charged versus logical size, quota failures, malformed commands, and compressed-file copies with deterministic results.
Process a sequence of file, search, quota, compression, and restoration operations against one in-memory cloud-storage service. Paths are opaque absolute strings. File content is bytes. The reserved admin user has unlimited quota; other users are charged by each file's current stored size. Failed operations are atomic, reads return logical content, and FIND supports only '*' as a wildcard.
Constraints
- At most 100,000 operations.
- Paths are nonempty absolute strings beginning with '/'.
- Non-admin quotas are positive integers.
- Compression size is between zero and logical size, inclusive.
- Malformed commands raise ValueError before mutation.
Examples
Input: {'operations': [['ADD_FILE','/a',b'abc'],['READ_FILE','/a'],['COPY_FILE','/a','/b'],['FIND','/*']]}
Expected Output: [True, b'abc', True, ['/a', '/b']]
Explanation: Basic add, read, copy, and wildcard search.
Input: {'operations': [['ADD_FILE','/x',b'x'],['ADD_FILE','/x',b'y'],['COPY_FILE','/missing','/z'],['READ_FILE','/none']]}
Expected Output: [True, False, False, None]
Explanation: Duplicate and missing paths fail without mutation.
Hints
- Keep O(1)-expected maps for paths, users, and per-owner usage.
- Store logical content separately from charged size and compressed state.
- A greedy two-pointer matcher is sufficient for a glob language containing only '*'.