← Questions

Party hours and quiet gaps by neighborhood and city

0
Scale AICodingLast asked Dec 2025

A community events app records every party in two separate lists that share a partyId:

  • partyWindows: rows of [partyId, start, end]. start and end are hours as strings, and a party runs over the half-open interval [start, end).
  • geoDataList: rows of [partyId, neighborhood, city].

Each neighborhood belongs to one city. Ignore a party that appears in only one of the two lists.

Implement a class PartyAggregator(partyWindows, geoDataList) with:

  1. neighborhood_hours() -> dict[str, int]: for each neighborhood, the number of hours during which at least one of its parties is running. Overlapping parties must not be counted twice.
  2. city_hours() -> dict[str, int]: the same measure for each city, over all parties in all of its neighborhoods.
  3. city_gaps() -> dict[str, list[tuple[int, int]]]: for each city, the quiet stretches (from, to) between its first party start and its last party end when no party is running anywhere in the city, in time order. A party ending at 15 and another starting at 15 leave no gap.
  4. report(): a list of (city, hours, gaps) sorted by hours descending, then city name ascending.

Raise ValueError for a window whose start is after its end. A zero-length window covers no time.

Constraints: up to 10^5 parties; hours fit in a 32-bit integer.

Example

partyWindows = [["a","10","14"], ["b","12","16"], ["c","20","21"], ["d","8","9"]]
geoDataList  = [["a","Harbor","Bayview"], ["b","Hilltop","Bayview"], ["c","Harbor","Bayview"], ["e","Old Town","Riverton"]]
  • neighborhood_hours() → {"Harbor": 5, "Hilltop": 4}
  • city_hours() → {"Bayview": 7} (10 to 16 is 6 hours, plus 20 to 21)
  • city_gaps() → {"Bayview": [(16, 20)]}
  • Party d has no location and e has no window, so both are ignored.

Reconstructed from the reported question; details may differ from what interviewers ask.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding