Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Parse and Transform Structured Data

MediumPython00:00
Practice interviewer
In session
5 left
00:00

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.

You need to log in / sign up to run or submit.

Problem

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.

Formal Specification

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.

Examples

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.

Constraints

  • 1 <= number of patients <= 10^4
  • 0 <= total observations <= 10^5
  • Patient IDs, codes, and timestamps are non-empty strings
  • Versions are positive integers
  • The input payload follows the specified structure

Function Signature

def transform_observations(payload):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output