Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Parse and Transform Structured Data
00:00
5 left

Parse and Transform Structured Data

MediumPython

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):
Interviewer

Your question is Parse and Transform Structured Data. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.