Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

API Data Transformation Function

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

Your question is API Data Transformation Function. 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

The Justworks People frontend receives employee records in an API-specific shape. Transform the response into department groups that are ready to render, while filtering inactive benefits, removing duplicate plans, and applying deterministic sorting.

Formal Specification

Implement transform_justworks_people(response). The input is a dictionary containing a data array. Each employee has id, first_name, last_name, employment_status, an optional department object with id and name, and a benefits array. Each benefit has plan_id, plan_name, and status.

Return a list of dictionaries with this shape:

  • department_id: the department ID, or null when the employee has no department
  • department: the department name, or "Unassigned" when there is no department
  • employees: transformed employee objects containing id, name, status, and active_plans

Include only benefits whose status is "active". Deduplicate benefits by plan_id, retain the first name encountered for each plan, and sort plan names alphabetically. Group employees by department, sort employees by last name, first name, then ID, and sort department groups by department name.

Constraints

  • 1 <= len(response["data"]) <= 10^5
  • Employee and department IDs are unique identifiers represented by strings or integers
  • Each employee has 0 <= len(benefits) <= 20 benefits
  • Each benefit has a non-empty plan_id and plan_name
  • Employee and department ordering must be deterministic

Function Signature

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