Idempotent Chunked Upload
medium · idempotency
Given total >= 0 and writes of [chunk_index, text], build an idempotent upload manifest. Indices in [0,total) are valid. Accept a new chunk or an identical retry (True); reject out-of-range indices or a conflicting retry (False), keeping the first contents. Return {'accepted': booleans, 'manifest': received indices sorted ascending, 'complete': whether all chunks exist, 'content': concatenated index-ordered chunks only when complete, else None}. Empty uploads are complete with content ''. Chunk text can be empty or Unicode. All writes are sequential fake storage operations.
Examples (JSON; null means Python None):
Input: {"total": 2, "writes": [[1, "b"], [0, "a"], [1, "b"]]}
Output: {"accepted": [true, true, true], "manifest": [0, 1], "complete": true, "content": "ab"}
Input: {"total": 2, "writes": [[0, "a"], [0, "x"], [2, "b"]]}
Output: {"accepted": [true, false, false], "manifest": [0], "complete": false, "content": null}
Starter code
def chunked_upload(total: int, writes: list[list]) -> dict:
"""Store each chunk once and assemble only a complete manifest."""
pass
Loading editor…
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python compiler