cswork / libraryOpen practice workspace

Coding questions · Airbnb

Find Hosts Meeting Listing, Rating, and Review-Coverage Rules

Find qualifying hosts with SQL using listing counts, a supplied rating cutoff, and exact recent-review coverage across their listings.

Find hosts who meet a super-host qualification rule involving listing count, average rating, and recent review coverage.

Input Tables

listings

ColumnTypeMeaning
listing_idINTEGERPrimary key
host_idINTEGERNon-null owner of the listing

reviews

ColumnTypeMeaning
review_idINTEGERPrimary key
listing_idINTEGERNon-null reference to listings.listing_id
ratingNUMERIC(3,2)Non-null rating from 1.00 through 5.00
review_dateDATENon-null review date

qualification_policy contains exactly one row:

ColumnTypeMeaning
as_of_dateDATENon-null date on which qualification is evaluated
minimum_average_ratingNUMERIC(3,2)Non-null cutoff from 1.00 through 5.00

Qualification Rules

The numeric average-rating cutoff is supplied by the policy table. Use the following explicit practice interpretation of the other conditions:

  1. A host has at least three listings in listings.
  2. The arithmetic mean of all individual reviews for those listings dated on or before as_of_date is strictly greater than minimum_average_rating.
  3. At least 90 percent of the host's listings each have at least one review in the inclusive 30-calendar-day window from as_of_date - 29 days through as_of_date.

A host with no reviews on or before the evaluation date does not qualify. Each listing has equal weight in the recent-review coverage fraction; each individual review has equal weight in the rating mean. These are exercise conventions, not a statement of any company's actual host policy.

Output Contract

Write one read-only PostgreSQL query returning a single column, host_id, containing qualifying hosts exactly once in ascending order.

Example

Let as_of_date be January 30 and minimum_average_rating be 4.50.

  • Host 10 has three listings. Each has one review dated January 10, and all three ratings are 5.00.
  • Host 20 has three listings. Two have one 5.00 review dated January 10; the third has no reviews.
  • Host 30 has three listings. Each has one review dated January 10, and all three ratings are 4.50.

Expected result:

host_id
10

Host 20 fails recent-review coverage. Host 30 fails the strict average-rating comparison.

Constraints and Clarifications

  • listings is the current ownership snapshot; there are no deleted listings or ownership changes to reconstruct.
  • Multiple recent reviews for one listing count only once toward coverage, but each contributes to the rating mean.
  • Older reviews still affect the rating mean, while reviews after as_of_date affect neither condition.
  • Compare the 90-percent requirement exactly; do not round a coverage percentage before applying it.
  • Preserve listings with zero reviews when counting the host's total listings.

Hint: Keep the denominators separate

The coverage condition counts listings, while the rating condition averages reviews. A direct join can multiply listing rows and silently change one of those denominators.

Super-Host Qualification

Table listings

columntypeconstraints
listing_idINTEGERPRIMARY KEY
host_idINTEGERNOT NULL

Sample data:

host_idlisting_id
1011
1012
1013
1024
1025
1036
1037
1038
1049
10410
10411
10412
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10733
10734
10735
10836
10837
10838
10939
10940
10941
11042
11043
11044

Table reviews

columntypeconstraints
review_idINTEGERPRIMARY KEY
listing_idINTEGERNOT NULL
ratingNUMERIC(3,2)NOT NULL
review_dateDATENOT NULL

Sample data:

listing_idratingreview_datereview_id
14.82026-01-201
14.62026-01-252
252026-01-183
34.92026-01-224
452026-01-125
452026-01-266
552026-01-147
552026-01-278
64.52026-01-109
74.52026-01-1010
84.52026-01-1011
952026-01-1512
1052026-01-1613
1152026-01-1714
134.92026-01-0115
144.92026-01-0516
154.92026-01-0817
164.92026-01-1118
174.92026-01-1419
184.92026-01-1720
194.92026-01-2021
204.92026-01-2422
214.92026-01-2823
2352026-01-0624
2452026-01-0925
2552026-01-1226
2652026-01-1527
2752026-01-1828
2852026-01-2129
2952026-01-2430
3052026-01-2731
3152025-12-3132
3252026-02-0133
3352026-01-1134
3452026-01-1235
3552026-01-1336
3312025-06-0137
364.62026-01-1938
374.62026-01-2039
384.62026-01-2140
3612026-02-1041
3952026-02-0342
4052026-02-0443
4152026-02-0544
4242026-01-0845
4242026-01-1546
4242026-01-2447
4352026-01-1948
4452026-01-2949

Table qualification_policy

columntypeconstraints
as_of_dateDATENOT NULL
minimum_average_ratingNUMERIC(3,2)NOT NULL

Sample data:

as_of_dateminimum_average_rating
2026-01-304.5

Expected output

host_id
101
105
108

Hints

  1. Keep the denominators separate: the coverage condition counts listings, while the rating condition averages reviews. A direct join can multiply listing rows and silently change one of those denominators.