Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Sorting Property Data by Criteria
00:00
5 left

Sorting Property Data by Criteria

EasyPython

Problem

Zillow listing cards can be sorted by several user-selected criteria, such as price, bedrooms, or address. Implement a function that returns the properties in the requested priority order.

Formal Specification

Write sort_properties(properties, criteria), where:

  • properties is a list of dictionaries. Each dictionary contains numeric fields price, bedrooms, bathrooms, and days_on_zillow, plus a string field address.
  • criteria is a non-empty list of dictionaries. Each criterion has a valid field and an order, either "asc" or "desc". Criteria are listed from highest to lowest priority.
  • Return a new list containing the same property dictionaries, sorted according to every criterion. Do not mutate the input list.
  • Sorting must be stable: properties equal under all criteria retain their original relative order.

Constraints

  • 1 <= len(properties) <= 10^5
  • 1 <= len(criteria) <= 5
  • Each criterion field exists in every property
  • Each criterion order is either "asc" or "desc"
  • Criterion fields are unique
  • Numeric property fields are non-negative

Function Signature

def sort_properties(properties, criteria):
Interviewer

Your question is Sorting Property Data by Criteria. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.