Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Grouping Objects by Property

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

Your question is Grouping Objects 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

In a Dassault Systèmes 3DEXPERIENCE interface, asset records may need to be displayed in groups such as design type, lifecycle state, or owner. Given an array of objects and a property name, group the objects by that property's value.

Implement group_by_property(items, property_name). The function must return a dictionary whose keys are the distinct property values and whose values are arrays of the original objects. Groups must appear in the order their keys are first encountered, and objects within each group must retain their original relative order. Do not mutate the input array or its objects.

Formal Specification

  • Input items: an array of dictionaries. Every dictionary contains property_name.
  • Input property_name: a string.
  • Each property value is hashable, such as a string, integer, boolean, or None.
  • Output: a dictionary mapping each distinct property value to an array of matching original dictionaries.

Constraints

  • 0 <= len(items) <= 10^5
  • Each object contains property_name
  • Property values are hashable
  • The input objects must not be mutated
  • Objects within each group preserve their original relative order

Function Signature

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