Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
Deduplicate Observations by Highest Version
00:00
5 left

Deduplicate Observations by Highest Version

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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.