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

Chunked aggregation over a large CSV

Medium100% solve itAsked 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

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

Example 1totals per region

Input · rows
amountregion
10.5APAC
2EU
1.5APAC
Output
{"EU": 2, "APAC": 12}

Example 2an unparseable amount is skipped

Input · rows
amountregion
5EU
not-a-numberEU
1EU
Output
{"EU": 6}

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.