KatInterview

Filter Hosts by Property Count, Rating, and Review Recency

SQL & DataAuto-graded · 16 test cases
MediumAuto-gradedSQL & Dataaggregationmulti-tabledate-filtering

Last updated

Tests multi-table aggregation with date-windowed filtering, distinct counting, and host-level qualification across inventory, ratings, and coverage.

Problem Statement

You are analyzing property rental platform data to identify high-quality hosts. Write a function that finds all hosts satisfying all three of the following conditions:

  1. The host owns at least 2 active properties (where is_active = True).
  2. The host's overall average rating, computed across every review they have ever received on any of their properties (active or inactive), is at least 4.50 (before rounding).
  3. Every one of the host's active properties has received at least one review between '2023-07-05' and '2024-01-01' (inclusive on both ends).

Input Format

listings: A list of dictionaries, each with:

  • listing_id (int): unique property identifier
  • host_id (int): owner identifier
  • is_active (bool): whether the property is currently active

reviews: A list of dictionaries, each with:

  • review_id (int): unique review identifier
  • listing_id (int): the property being reviewed (may or may not exist in listings)
  • rating (float): guest rating, a value between 1.0 and 5.0
  • review_date (str): date in ISO format "YYYY-MM-DD"

Output Format

Return a list of lists, where each inner list contains exactly three elements:

  1. host_id (int)
  2. active_property_count (int): the number of active properties owned by this host
  3. mean_rating (float): the overall average rating across all of the host's reviews, rounded to exactly 2 decimal places

Sort order: by mean_rating descending, then by host_id ascending (both applied to the rounded rating for sorting purposes).

Constraints

  • 1 ≤ number of listings ≤ 10,000
  • 0 ≤ number of reviews ≤ 100,000
  • All review_date strings are valid ISO 8601 dates
  • All ratings are between 1.0 and 5.0 inclusive
  • A property may have zero, one, or many reviews
  • Reviews may reference listing_id values not present in listings (these reviews should be ignored)
  • The 180-day recency window is fixed: from '2023-07-05' through '2024-01-01' inclusive

Example

Input:

listings = [
    {"listing_id": 101, "host_id": 1, "is_active": True},
    {"listing_id": 102, "host_id": 1, "is_active": True},
    {"listing_id": 103, "host_id": 2, "is_active": True},
    {"listing_id": 104, "host_id": 2, "is_active": True},
    {"listing_id": 105, "host_id": 3, "is_active": True}
]

reviews = [
    {"review_id": 1, "listing_id": 101, "rating": 4.8, "review_date": "2023-11-10"},
    {"review_id": 2, "listing_id": 102, "rating": 4.6, "review_date": "2023-12-01"},
    {"review_id": 3, "listing_id": 103, "rating": 4.9, "review_date": "2023-08-15"},
    {"review_id": 4, "listing_id": 104, "rating": 4.7, "review_date": "2022-01-10"},
    {"review_id": 5, "listing_id": 105, "rating": 5.0, "review_date": "2023-10-05"}
]

Output:

[[1, 2, 4.70]]

Explanation:

  • Host 1: owns 2 active properties (101, 102). Overall average rating = (4.8 + 4.6) / 2 = 4.70 ≥ 4.50. Both active properties have reviews within the window (2023-11-10 and 2023-12-01). All conditions met. ✓
  • Host 2: owns 2 active properties (103, 104). Overall average rating = (4.9 + 4.7) / 2 = 4.80 ≥ 4.50. However, property 104's only review is dated 2022-01-10, which is outside the window. Only 1 out of 2 active properties (50%) has a recent review. Fails condition 3. ✗
  • Host 3: owns only 1 active property (105). Fails condition 1 (need ≥ 2). ✗

Important Rules and Edge Cases

  • Rating calculation scope: The average rating includes all reviews on all of the host's properties, regardless of whether those properties are currently active or inactive, and regardless of review date. Only conditions 1 and 3 are restricted to active properties.
  • Recency coverage: An active property counts as "covered" if it has at least one review with review_date between '2023-07-05' and '2024-01-01' inclusive. If a property has multiple reviews in the window, it still counts as one covered property (no double-counting).
  • Unreviewed properties: An active property with zero reviews ever fails condition 3 (it is not covered).
  • Rating threshold: The 4.50 threshold is applied to the unrounded average. A host with an average of 4.495 does not qualify, even though rounding would display 4.50.
  • Orphan reviews: Reviews with a listing_id that does not appear in listings are silently ignored.
  • Hosts with zero reviews: A host with no reviews at all cannot have an average rating ≥ 4.50 and should be excluded.
  • Inactive properties: Do not count toward the active property count or coverage requirement, but their reviews do contribute to the overall average rating.

Examples

Example 1

Input: filter_qualified_hosts([{'listing_id': 101, 'host_id': 1, 'is_active': True}, {'listing_id': 102, 'host_id': 1, 'is_active': True}, {'listing_id': 103, 'host_id': 2, 'is_active': True}, {'listing_id': 104, 'host_id': 2, 'is_active': True}, {'listing_id': 105, 'host_id': 3, 'is_active': True}], [{'review_id': 1, 'listing_id': 101, 'rating': 4.8, 'review_date': '2023-11-10'}, {'review_id': 2, 'listing_id': 102, 'rating': 4.6, 'review_date': '2023-12-01'}, {'review_id': 3, 'listing_id': 103, 'rating': 4.9, 'review_date': '2023-08-15'}, {'review_id': 4, 'listing_id': 104, 'rating': 4.7, 'review_date': '2022-01-10'}, {'review_id': 5, 'listing_id': 105, 'rating': 5, 'review_date': '2023-10-05'}])

Output: [[1, 2, 4.7]]

Hints

Hint 1

Calculate the three conditions separately: count active properties per host, compute overall average ratings across all reviews, and track which active properties have at least one recent review.

Hint 2

Use a set to record which active properties have recent reviews, ensuring that multiple reviews on the same property don't inflate the coverage count.

Hint 3

Remember that the rating average includes reviews from all properties (active and inactive, any date), while the recency requirement only applies to active properties within the 180-day window.