Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Efficient Grouping by Property

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

Your question is Efficient Grouping by Property. 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 Supermicro Server Dashboard receives an array of record objects, such as server health or inventory records. Implement a function that groups the records by a specified property without changing their order within each group.

The groups must appear in the order their property values are first encountered. Each record is a Python dictionary, and the grouping property is guaranteed to exist and contain a hashable value. Do not mutate the input list or its records.

Formal Specification

Implement group_by_property(records, property_name).

  • Input: records, a list of dictionaries, and property_name, a string identifying a dictionary key.
  • Output: A dictionary mapping each distinct property value to a list of records containing that value.
  • Records must remain in their original relative order inside each group.
  • The returned dictionary must preserve first-seen group order, as Python dictionaries do.

Constraints

  • 0 <= len(records) <= 10^5
  • 1 <= len(property_name) <= 100
  • Every record contains property_name
  • Every property value is hashable
  • Do not mutate the input records or list

Function Signature

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