KatInterview

Merge Event Windows Within Each Locality

Greedy & IntervalsAuto-graded · 16 test cases
MediumAuto-gradedGreedy & Intervalsintervalsmerge-intervalshash-map

Last updated

Join two record lists by id, group event time ranges by locality, merge overlapping or touching ranges, and return the merged windows in a specified sort order.

You are given two lists of dictionaries describing gatherings and where they took place.

Events (first argument). Each dictionary has:

  • party_id: a string or an integer identifying the event
  • start_timestamp: when the event begins
  • end_timestamp: when the event ends; it is always strictly later than start_timestamp

Places (second argument). Each dictionary has:

  • party_id: matches an event's id
  • neighborhood: a string
  • city: a string
  • state: a string

Every party_id appears exactly once in the events list and exactly once in the places list.

A locality is the full triple (neighborhood, city, state). Two locations that share a neighborhood name but differ in city or state are different localities.

For a given locality, an active window is a maximal continuous stretch of time during which at least one event in that locality is in progress. Two events in the same locality belong to the same window if their time ranges overlap, or if one ends at exactly the moment the other starts.

Task

Write a function that combines the two lists and returns every active window for every locality.

Output

Return a list of dictionaries, one per active window, each with these keys:

  • neighborhood
  • city
  • state
  • block_start_timestamp: the earliest start in the window
  • block_end_timestamp: the latest end in the window

Sort the returned list by state, then city, then neighborhood, then block_start_timestamp, all in ascending order. If there are no events, return an empty list.

Timestamps are mutually comparable values of one consistent type within an input (for example integers, or ISO-formatted strings that sort chronologically). Return them exactly as given, without converting them.

Constraints

  • Up to 100,000 events in total.
  • The events list may be in any order.
  • end_timestamp > start_timestamp for every event.
  • Each party_id has exactly one matching entry in each list.
  • Your solution should run in about O(n log n) time.

Examples

Example 1

Input: merge_locality_windows([{'party_id': 'p1', 'start_timestamp': 1, 'end_timestamp': 4}, {'party_id': 'p2', 'start_timestamp': 4, 'end_timestamp': 7}, {'party_id': 'p3', 'start_timestamp': 9, 'end_timestamp': 12}], [{'party_id': 'p1', 'neighborhood': 'North', 'city': 'Metro', 'state': 'CA'}, {'party_id': 'p2', 'neighborhood': 'North', 'city': 'Metro', 'state': 'CA'}, {'party_id': 'p3', 'neighborhood': 'North', 'city': 'Metro', 'state': 'CA'}])

Output: [{'neighborhood': 'North', 'city': 'Metro', 'state': 'CA', 'block_start_timestamp': 1, 'block_end_timestamp': 7}, {'neighborhood': 'North', 'city': 'Metro', 'state': 'CA', 'block_start_timestamp': 9, 'block_end_timestamp': 12}]

Example 2

Input: merge_locality_windows([{'party_id': 1, 'start_timestamp': '2024-01-01T20:00:00', 'end_timestamp': '2024-01-01T22:00:00'}, {'party_id': 2, 'start_timestamp': '2024-01-01T19:00:00', 'end_timestamp': '2024-01-01T21:00:00'}], [{'party_id': 1, 'neighborhood': 'Central', 'city': 'Oakland', 'state': 'CA'}, {'party_id': 2, 'neighborhood': 'Central', 'city': 'Oakland', 'state': 'CA'}])

Output: [{'neighborhood': 'Central', 'city': 'Oakland', 'state': 'CA', 'block_start_timestamp': '2024-01-01T19:00:00', 'block_end_timestamp': '2024-01-01T22:00:00'}]

Hints

Hint 1

Start by building a dictionary from party_id to the (neighborhood, city, state) triple so that each event can be matched to its locality in constant time.

Hint 2

Group the (start, end) pairs by locality, then handle each group on its own. Sort a group by start time so a single left-to-right pass is enough to merge it.

Hint 3

While sweeping, extend the current window when the next start is less than or equal to the current window's end, and keep the larger of the two ends. Finally, sort all resulting records by (state, city, neighborhood, window start).