Parse JSON Accounts into Typed Python Objects and Prepare Features
DaveMachine Learning Engineer · OnsiteLast updated
This problem tests your ability to sanitize and transform raw JSON account data into a structured format while enforcing strict numerical integrity and data consistency.
Problem Statement
You are provided with a JSON string representing a snapshot of bank accounts. Each object in the list contains an account_id (string), a balance (numeric), and an updated_at (ISO 8601 string).
Implement a function process_accounts(json_data: str) -> dict that performs the following steps:
- Validation: Filter out any records with non-finite balances (e.g.,
NaN,Inf) or missing fields. - Deduplication: If multiple records exist for the same
account_id, retain only the record with the most recentupdated_attimestamp. - Aggregation: Return a dictionary containing two keys:
total_balance: The sum of all unique, valid account balances.account_map: A dictionary mappingaccount_idto its validatedbalance.
Constraints
- The input string may contain thousands of records.
balancevalues must be treated asDecimaltypes to avoid floating-point errors.- The output
total_balanceshould be returned as a float, rounded to two decimal places.
Example
input_json = """
[
{"account_id": "A1", "balance": 100.50, "updated_at": "2023-01-01T10:00:00Z"},
{"account_id": "A1", "balance": 150.25, "updated_at": "2023-01-02T10:00:00Z"},
{"account_id": "A2", "balance": "invalid", "updated_at": "2023-01-02T10:00:00Z"}
]
"""
# Result: {"total_balance": 150.25, "account_map": {"A1": 150.25}}
Hints
Hint 1
Use the `json` module to parse the input and the `decimal` module to handle financial values accurately.
Hint 2
Store the records in a dictionary keyed by `account_id` to naturally handle deduplication as you iterate through the list.
Hint 3
Parse the `updated_at` strings into datetime objects using `datetime.fromisoformat()` to ensure accurate chronological comparison.
Follow-up questions
- How would you modify your implementation to process a multi-gigabyte JSON file that does not fit in memory?
- If this pipeline were to fail halfway through a large batch, what strategy would you use to ensure atomicity and idempotency?
- How would you validate that the schema of the incoming JSON matches the expected structure before processing begins?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.