Your question is Parse and Transform Structured Data. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Sanofi's clinical-data ingestion pipeline receives nested payloads containing patients and their observations. Write a function that transforms the payload into a normalized list, keeps only the highest version of duplicate observations, and returns records in a deterministic order.
An observation is identified by the combination of patient_id, code, and collected_at. If multiple observations share that key, retain the one with the greatest version.
Implement transform_observations(payload). The input is a dictionary with a patients key whose value is a list of patient dictionaries. Each patient contains a string patient_id and an observations list. Each observation contains code, value, unit, collected_at, and integer version fields.
Return a list of dictionaries. Each output dictionary must contain exactly patient_id, code, value, unit, and collected_at, excluding version. Sort the result by patient_id, then by collected_at, then by code, all in ascending order.
Example 1
Input: {"patients": [{"patient_id": "P2", "observations": [{"code": "TEMP", "value": 37.1, "unit": "C", "collected_at": "2026-01-02", "version": 1}]}]}
Output: [{"patient_id": "P2", "code": "TEMP", "value": 37.1, "unit": "C", "collected_at": "2026-01-02"}]
The output removes version while preserving the observation data.
Example 2
Two observations with the same identity and versions 1 and 3 produce only the version 3 record.
def transform_observations(payload):