Back to Practice
#0155

Keep Latest Record per Entity

MediumPandas25 min20 XP

Problem

Given rows with id, updated_at, and value, return the latest row for each id sorted by id.

Why This Matters

Latest-record logic shows up in user profiles, slowly changing dimensions, model features, and event snapshots.

Function Signature

def latest_records(df):

Examples

Example 1
Inputid 1 appears on Jan 1 and Jan 3; id 2 appears on Jan 2
Outputid 1 keeps Jan 3; id 2 keeps Jan 2

Sorting by updated_at makes keep="last" choose the newest row per id.

Constraints

  • Return the exact requested value.
  • Handle empty or small inputs when the prompt allows them.
  • Keep the code readable and deterministic.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputid 1 appears on Jan 1 and Jan 3; id 2 appears on Jan 2
Expectedid 1 keeps Jan 3; id 2 keeps Jan 2

Sorting by updated_at makes keep="last" choose the newest row per id.

Hidden Test Categories
empty or smallest inputduplicates or repeated valueslarger realistic input