← Problems23. Chunked aggregation over a large CSVMediumPython
00:00 / 20:00

Chunked aggregation over a large CSV

Medium·Acceptance ·Asked at Amazon, PayPal

Aggregate revenue per region from a file too large to hold in memory. You are given an iterator of rows, not a list, and the harness measures peak memory.

Input schema

solve(rows: Iterator[dict]) -> dict[str, float] each row: {"region": str, "amount": str} # amount arrives as text

Example

rows -> {"region": "APAC", "amount": "10.5"}, {"region": "EU", "amount": "2"}, {"region": "APAC", "amount": "1.5"} Output -> {"APAC": 12.0, "EU": 2.0}

Constraints

  • Rows with an unparseable amount are skipped, not fatal
  • Peak memory limit 256 MB against a 40,000,000-row input
  • Materializing the iterator into a list fails the memory case
  • Amounts round to two decimals in the result

Topics

aggregation

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8 · ⌘↵ run, ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.