KatInterview

Where Is Everyone? Locating Travelers at a Given Instant

Heaps, Sorting & SearchAuto-graded · 18 test cases
MediumAuto-gradedHeaps, Sorting & Searchsortinghash-mapintervals

Last updated

Group unsorted flight records by traveler and decide each traveler's status at a query timestamp, using half-open flight intervals.

You are given a log of flights taken by a group of travelers. The log is unsorted. Each entry is a dictionary with these string fields:

  • departure_airport
  • departure_time
  • arrival_airport
  • arrival_time
  • user_id

All times are ISO-8601 UTC strings in the same fixed format (for example 2024-01-01T08:00:00Z), so comparing two of them as plain strings gives their chronological order. The flights of any single traveler never overlap in time.

Given a query timestamp time (same format), report the status of every traveler that appears in the log at that moment.

Status rules

For one traveler, consider their flights ordered by departure time:

  • Before any departure. If time is earlier than the traveler's first departure, the status is the string "unknown".
  • In the air. If departure_time <= time < arrival_time for some flight, the status is the list ["in_transit", departure_airport, arrival_airport] for that flight.
  • On the ground. Otherwise, the traveler is at the arrival airport of their most recently completed flight. This covers both having just landed and waiting for the next departure. The status is that airport code as a string.

Flight intervals are half-open: at exactly the departure time the traveler is already in transit, and at exactly the arrival time the traveler has landed and is at the arrival airport.

Output

Return a dictionary mapping each user_id found in the log to that traveler's status (a string or a 3-element list, as described above). Every traveler present in the log must appear in the result, including those whose status is "unknown". If the log is empty, return an empty dictionary.

Constraints

  • The number of flight records can be large, so aim for better than quadratic time.
  • Timestamps share one fixed ISO-8601 UTC format, so string comparison matches time order.
  • Flights belonging to the same user_id do not overlap.
  • Each record contains all five fields as non-empty strings.
  • The input list may be in any order.

Examples

Example 1

Input: locate_travelers([{'departure_airport': 'JFK', 'departure_time': '2024-01-01T08:00:00Z', 'arrival_airport': 'LAX', 'arrival_time': '2024-01-01T11:00:00Z', 'user_id': 'u1'}, {'departure_airport': 'SFO', 'departure_time': '2024-01-01T09:00:00Z', 'arrival_airport': 'SEA', 'arrival_time': '2024-01-01T10:30:00Z', 'user_id': 'u2'}], '2024-01-01T09:30:00Z')

Output: {'u1': ['in_transit', 'JFK', 'LAX'], 'u2': ['in_transit', 'SFO', 'SEA']}

Example 2

Input: locate_travelers([{'departure_airport': 'JFK', 'departure_time': '2024-01-01T08:00:00Z', 'arrival_airport': 'LAX', 'arrival_time': '2024-01-01T11:00:00Z', 'user_id': 'u1'}, {'departure_airport': 'LAX', 'departure_time': '2024-01-01T13:00:00Z', 'arrival_airport': 'ORD', 'arrival_time': '2024-01-01T16:00:00Z', 'user_id': 'u1'}], '2024-01-01T12:00:00Z')

Output: {'u1': 'LAX'}

Example 3

Input: locate_travelers([{'departure_airport': 'JFK', 'departure_time': '2024-01-01T08:00:00Z', 'arrival_airport': 'LAX', 'arrival_time': '2024-01-01T11:00:00Z', 'user_id': 'u1'}], '2024-01-01T11:00:00Z')

Output: {'u1': 'LAX'}

Hints

Hint 1

Travelers are independent of each other, so start by bucketing the records by user_id.

Hint 2

Within one traveler's bucket, sort by departure time, then walk through the flights while tracking the current status, starting from "unknown".

Hint 3

For each flight: if the query time is before its departure, stop; if it is before the arrival, the traveler is in transit and you stop; otherwise they are now at the arrival airport and you continue.