KatInterview

Grouped Median Property Price by Region

Heaps, Sorting & SearchAuto-graded · 18 test cases
MediumAuto-gradedHeaps, Sorting & Searchsortinggroupingmedian

Last updated

Tests ability to compute per-group medians over a list of records, correctly handling odd/even counts, duplicate values, and half-up rounding to two decimal places.

Problem

You are given a collection of property listings, each tagged with a region name and an asking price. Your task is to find the median asking price for every region and return the results in alphabetical order by region name.

The median is defined as follows:

  • Sort all prices within a region in ascending order.
  • If the region has an odd number of listings, the median is the single middle price.
  • If the region has an even number of listings, the median is the arithmetic mean of the two middle prices.

Round every median to two decimal places, using half-up rounding (0.005 rounds to 0.01).

Input

A list of dictionaries. Each dictionary represents one listing and contains:

KeyTypeDescription
"id"intUnique listing identifier
"city"strRegion name (non-null)
"price"int or floatAsking price (positive, non-null)

Output

A list of dictionaries, one per region, sorted alphabetically by "city". Each dictionary contains:

KeyTypeDescription
"city"strRegion name
"median_price"floatMedian price rounded to two decimal places

Constraints

  • The input list has between 1 and 10^6 entries.
  • Every region contains at least one listing.
  • price is a positive number; it will not be zero or negative.
  • Do not deduplicate prices. If a region has three listings all priced at 100, the median is 100.
  • Prices may have up to two decimal places (e.g., 10.01).
  • When two middle prices average to a value ending in exactly 0.005, round up (away from zero) to the nearest cent.
  • Return regions in ascending lexicographic order by "city".

Examples

Example 1

Input: median_house_price([{'id': 1, 'city': 'Austin', 'price': 100}, {'id': 2, 'city': 'Austin', 'price': 200}, {'id': 3, 'city': 'Austin', 'price': 300}, {'id': 4, 'city': 'Austin', 'price': 400}, {'id': 5, 'city': 'San Francisco', 'price': 150}, {'id': 6, 'city': 'San Francisco', 'price': 250}, {'id': 7, 'city': 'San Francisco', 'price': 350}, {'id': 8, 'city': 'Seattle', 'price': 500}])

Output: [{'city': 'Austin', 'median_price': 250}, {'city': 'San Francisco', 'median_price': 250}, {'city': 'Seattle', 'median_price': 500}]

Example 2

Input: median_house_price([{'id': 1, 'city': 'Boston', 'price': 100}, {'id': 2, 'city': 'Boston', 'price': 100}, {'id': 3, 'city': 'Boston', 'price': 125}, {'id': 4, 'city': 'Chicago', 'price': 80}])

Output: [{'city': 'Boston', 'median_price': 100}, {'city': 'Chicago', 'median_price': 80}]

Example 3

Input: median_house_price([{'id': 1, 'city': 'New York', 'price': 10.01}, {'id': 2, 'city': 'New York', 'price': 10.02}, {'id': 3, 'city': 'New York', 'price': 10.03}, {'id': 4, 'city': 'New York', 'price': 10.04}])

Output: [{'city': 'New York', 'median_price': 10.03}]

Hints

Hint 1

Group all prices by region name first, then sort each group independently — you only need order statistics within a group, not a global sort.

Hint 2

For a group of size n, the middle index (1-based) is n//2 for odd n. For even n, you need the average of the elements at 0-based indices n//2 - 1 and n//2 after sorting.

Hint 3

Python's built-in round() uses banker's rounding, which can give wrong results for exact half-cent values. Use the Decimal module with ROUND_HALF_UP to match the expected output precisely.