PythonStudio
Interview Problems

Parallel Processing over Shards

medium · map reduce

Each shard is a list of integers. completions is a possibly duplicated order of shard indices returned by simulated workers; all indices are valid. Count each completed shard at most once. Return {'partials': per-shard sum or None if never completed, 'total': sum of completed partials, 'complete': all shards completed}. Return partials in shard-index order, never completion order. Negative values and empty shards are valid. This deterministic reducer does not start real workers. Examples (JSON; null means Python None): Input: {"shards": [[1, 2], [4], [-1, 3]], "completions": [2, 0, 2, 1]} Output: {"partials": [3, 4, 2], "total": 9, "complete": true} Input: {"shards": [[], [2]], "completions": [0, 0]} Output: {"partials": [0, null], "total": 0, "complete": false}

Starter code

def reduce_shards(shards: list[list[int]], completions: list[int]) -> dict:
    """Reduce retryable shard completions exactly once."""
    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