Engineering practice · 30 MIN
Project · Parse a stream of event records
Separate parsing, validation and aggregation.
An ingestion pipeline should decide whether one malformed record stops the batch. Here we skip invalid lines and aggregate valid values with a generator. Catch only expected parsing exceptions so programming bugs remain visible. Python bool subclasses int; explicitly exclude it from integer-valued events. Although this lesson receives an in-memory list, a generator can also consume a file one line at a time.
Treat the function as a small service: define a contract, maintain an invariant, and test the boundaries.
Read the example
print(isinstance(True, int))
Check the expected output
True
Your challenge
Input is a list of JSON strings. For each decoded object with a string category and an integer amount (excluding booleans), add amount to that category. Skip malformed JSON and invalid records. Return totals as an object.
Solution cost: O(B), B total input bytes, plus expected constant-time dictionary updates. time · O(c + L), c categories and L largest decoded record; input list excluded. space
Common trap
except Exception silently hides errors in your own aggregation code.
Further reading: Python JSON