KatInterview

Park Ticket Revenue and Pass-Holder Visit Averages

SQL & DataAuto-graded · 4 test cases
MediumAuto-gradedSQL & Dataaggregationjoinsdate-range-filtering

Last updated

Aggregate ticket revenue and shares, then compute average distinct in-period visits per annual-pass period, including zero-visit passes, to project total entries.

Park Ticket Revenue and Pass-Holder Visit Averages

An amusement park keeps four datasets. Implement a function that turns them into a revenue breakdown and a projection of total park entries. It receives plain Python lists of dictionaries instead of database tables, but the logic is the same as the equivalent SQL joins and aggregations.

Inputs

  1. ticket_catalog: a list of dicts, one per ticket type, with keys:
    • id (int)
    • name (str)
    • price_usd (int)
    • duration_days (int)
  2. units_sold_rows: a list of dicts with keys ticket_type_id (int) and units_sold (int). It gives the number of units sold in the last fiscal year. Each ticket type appears at most once, and every ticket_type_id exists in ticket_catalog. A catalog type with no row sold 0 units.
  3. pass_periods: a list of dicts with keys user_id, start_date and end_date. Dates are ISO strings ("YYYY-MM-DD"). Each dict is one annual pass. A user may appear several times, once per pass.
  4. visit_log: a list of dicts with keys user_id and visit_date (ISO string). One user may have many visits, including several records on the same day.

What to compute

Revenue. For each ticket type, revenue is price_usd * units_sold. The revenue share is that revenue divided by total revenue across all types. If total revenue is 0, every share is 0.0.

Entries model.

  • Every ticket type whose name is not "annual_pass" gives duration_days entries per unit sold. Sum units_sold * duration_days over these types to get constant_entries.
  • The ticket type named "annual_pass" gives an unknown number of entries per unit. Report its total units sold as annual_pass_units. If no such type has sales, this is 0.

Average visits per pass period. For each element of pass_periods, count the distinct visit dates of the same user that fall within [start_date, end_date], with both ends inclusive. Visits outside the period do not count. Several records on the same date count once. Treat each pass period as its own holder, even if the same user has several. Periods with zero qualifying visits stay in the average. Average the counts over all pass periods. If there are no pass periods, the average is 0.0.

Total entries. total_entries = constant_entries + annual_pass_units * average_visits.

Output

Return a dict with exactly these keys:

  • "revenue_by_ticket_type": a list ordered by ticket type id ascending. Each item is a dict {"ticket_type": name, "revenue_usd": revenue, "revenue_share": share}.
  • "total_revenue_usd": the sum of all revenues.
  • "entries_as_function_of_x": a dict {"constant_entries": ..., "annual_pass_units": ...}.
  • "average_visits_per_pass_period": the average from above, as a float.
  • "total_entries": the projection from above.

Constraints

  • Up to roughly 10^3 ticket types, 10^5 pass periods and 10^6 visit records.
  • All dates are valid ISO dates, and start_date <= end_date for every pass.
  • units_sold, price_usd and duration_days are non-negative integers.
  • Visits by users with no pass period contribute nothing.

Examples

Example 1

Input: summarize_park_sales_and_passes([{'id': 1, 'name': 'single_day', 'price_usd': 80, 'duration_days': 1}, {'id': 2, 'name': 'five_day', 'price_usd': 300, 'duration_days': 5}, {'id': 3, 'name': 'annual_pass', 'price_usd': 1000, 'duration_days': 365}], [{'ticket_type_id': 1, 'units_sold': 250000}, {'ticket_type_id': 2, 'units_sold': 100000}, {'ticket_type_id': 3, 'units_sold': 10000}], [{'user_id': 1, 'start_date': '2024-03-15', 'end_date': '2025-03-14'}, {'user_id': 5, 'start_date': '2024-08-21', 'end_date': '2025-08-20'}], [{'user_id': 1, 'visit_date': '2024-06-10'}, {'user_id': 1, 'visit_date': '2024-06-11'}, {'user_id': 1, 'visit_date': '2024-07-01'}, {'user_id': 5, 'visit_date': '2024-09-01'}, {'user_id': 5, 'visit_date': '2024-09-15'}, {'user_id': 2, 'visit_date': '2024-06-15'}])

Output: {'revenue_by_ticket_type': [{'ticket_type': 'single_day', 'revenue_usd': 20000000, 'revenue_share': 0.3333333333333333}, {'ticket_type': 'five_day', 'revenue_usd': 30000000, 'revenue_share': 0.5}, {'ticket_type': 'annual_pass', 'revenue_usd': 10000000, 'revenue_share': 0.16666666666666666}], 'total_revenue_usd': 60000000, 'entries_as_function_of_x': {'constant_entries': 750000, 'annual_pass_units': 10000}, 'average_visits_per_pass_period': 2.5, 'total_entries': 775000}

Hints

Hint 1

Revenue is a per-type product, and the shares need the grand total, so compute revenues first and divide in a second pass.

Hint 2

Group the visit dates by user into sets so duplicates on the same day collapse, then check each pass period against only its own user's dates.

Hint 3

Iterate over pass periods, not over visits. That keeps zero-visit passes in the denominator, and a user with two passes counts twice.