Where Is Everyone? Locating Travelers at a Given Instant
Heaps, Sorting & SearchAuto-graded · 18 test casesLast 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_airportdeparture_timearrival_airportarrival_timeuser_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
timeis earlier than the traveler's first departure, the status is the string"unknown". - In the air. If
departure_time <= time < arrival_timefor 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_iddo 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each traveler's history is independent, so first group the records by user_id. Then handle each group on its own:
- Sort that traveler's flights by departure time.
- Start with the status
"unknown". - Walk through the flights in order:
- If
timeis earlier than the flight's departure, the traveler has not taken this flight yet. Stop; the current status stands. - If
timeis earlier than the flight's arrival (and not earlier than the departure), the traveler is airborne. Set the status to["in_transit", departure_airport, arrival_airport]and stop. - Otherwise the flight is finished, so the traveler is at its arrival airport. Record that and move on to the next flight.
- If
- Store the final status under the traveler's
user_id.
Because the timestamps share one fixed ISO-8601 UTC format, plain string comparison gives chronological order, so no date parsing is needed.
Why it is correct
A traveler's flights do not overlap, so after sorting, the intervals [departure, arrival) are disjoint and ordered. For any query time exactly one case applies: it falls before the first departure, inside one flight's interval, or in the gap after a landing and before the next departure (or after the final landing). The loop keeps the invariant that the status is correct for the gap preceding the flight being examined. A finished flight moves the status to its arrival airport, which is the correct status for the following gap. "Just landed" and "waiting to depart" are the same state, namely the last arrival airport.
The half-open interval rule handles the boundaries. At time == departure the check time < departure fails and time < arrival holds, so the traveler is in transit. At time == arrival the check time < arrival fails, so the traveler is at the arrival airport. For back-to-back flights where one arrival equals the next departure, the next flight's in-transit rule takes effect at that instant.
Complexity
With n flights in total, grouping takes O(n) and sorting every group costs at most O(n log n). The scan is O(n). Overall time is O(n log n) and space is O(n).
Reference solution
from collections import defaultdict
from typing import Dict, List
def locate_travelers(flights: List[Dict[str, str]], time: str) -> Dict[str, object]:
by_user = defaultdict(list)
for flight in flights:
by_user[flight["user_id"]].append(flight)
result = {}
for user_id, user_flights in by_user.items():
user_flights.sort(key=lambda f: f["departure_time"])
status = "unknown"
for f in user_flights:
if time < f["departure_time"]:
break
if time < f["arrival_time"]:
status = ["in_transit", f["departure_airport"], f["arrival_airport"]]
break
status = f["arrival_airport"]
result[user_id] = status
return result