Where Is the Traveler? Locate by Timestamp from Trip Logs
Heaps, Sorting & SearchAuto-graded · 19 test casesLast updated
Answer timestamp queries against unsorted trip intervals using sorting and binary search, handling closed boundaries, gaps between trips, and times before the first trip.
Where Is the Traveler?
You have the complete trip log for a single traveler, given as a list of trip records in no particular order. You are also given a list of timestamps. For each timestamp, report the traveler's status.
Each trip record is a dictionary with these keys:
departure_airport: the code of the airport the trip leaves fromarrival_airport: the code of the airport the trip lands atdeparture_time: an integer timestamp at which the trip beginsarrival_time: an integer timestamp at which the trip ends
Task
Implement a function that takes the list of trips and the list of query timestamps, and returns a list with one answer per query, in the same order as the queries.
For a query timestamp t, the answer is determined as follows:
- If
tlies in a trip's closed interval[departure_time, arrival_time](both endpoints included), the answer is the string"in-flight". - Otherwise, if
tis later than the end of some trip and earlier than the start of the next one (or no trip follows), the answer is thearrival_airportof the most recent trip that has already ended. - Otherwise, if
tis earlier than the departure of every trip, the answer isNone.
If a trip departs at the exact moment the previous one lands, a query at that moment is "in-flight", because the closed interval of the later trip includes it.
A trip may have departure_time == arrival_time. Its interval then contains just that single timestamp.
Constraints
- The trips are not guaranteed to be sorted.
- All timestamps (in trips and queries) are integers.
- The traveler is never on two trips at once: when sorted by departure, each trip's
departure_timeis at least the previous trip'sarrival_time. - The trips list may be empty, in which case every answer is
None. - Queries may repeat and need not be sorted; each is answered independently.
- The output list has the same length and order as
queries. Each element is"in-flight", an airport code string, orNone. - Do not modify the input lists.
Examples
Example 1
Input: locate_traveler([{'departure_airport': 'JFK', 'arrival_airport': 'LAX', 'departure_time': 100, 'arrival_time': 200}, {'departure_airport': 'LAX', 'arrival_airport': 'SFO', 'departure_time': 300, 'arrival_time': 400}], [150, 250, 350, 450])
Output: ['in-flight', 'LAX', 'in-flight', 'SFO']
Example 2
Input: locate_traveler([{'departure_airport': 'SFO', 'arrival_airport': 'SEA', 'departure_time': 500, 'arrival_time': 650}, {'departure_airport': 'JFK', 'arrival_airport': 'LAX', 'departure_time': 100, 'arrival_time': 200}, {'departure_airport': 'LAX', 'arrival_airport': 'SFO', 'departure_time': 300, 'arrival_time': 400}], [50, 100, 200, 201, 300, 400, 651])
Output: [None, 'in-flight', 'in-flight', 'LAX', 'in-flight', 'in-flight', 'SEA']
Example 3
Input: locate_traveler([{'departure_airport': 'A', 'arrival_airport': 'B', 'departure_time': 10, 'arrival_time': 10}], [0, 10, 11])
Output: [None, 'in-flight', 'B']
Hints
Hint 1
Sort the trips by departure time once so the timeline is in order. For any query, only one trip can be relevant.
Hint 2
For a query t, the relevant trip is the last one whose departure_time is at most t. Find it with binary search over the sorted departure times (bisect_right minus one).
Hint 3
Once you have that trip: no such trip means None; t <= its arrival_time means 'in-flight'; otherwise return its arrival_airport. Both interval ends are inclusive.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sort a copy of the trips by departure_time and build a parallel array of departure times. For each query t, the only trip that can matter is the last one that departed at or before t. Find its index with bisect_right(starts, t) - 1.
Then decide the answer from that trip:
- If no such trip exists (index is
-1),tis before every trip, so the answer isNone. - If
t <= arrival_time, thentlies in the closed interval[departure_time, arrival_time], so the answer is"in-flight". - Otherwise the trip has already ended and the next one (if any) has not yet started, so the traveler is at that trip's
arrival_airport.
Why it is correct
Because the traveler is never on two trips at once, the sorted trips form non-overlapping intervals, and every timestamp falls into exactly one region: before the first departure, inside a trip, in a gap between trips, or after the last trip. Any trip departing after t cannot affect the answer, and any earlier trip than the last-departed one has ended before that one began, so it is irrelevant. bisect_right includes trips departing exactly at t, which makes the departure endpoint inclusive. The <= comparison makes the arrival endpoint inclusive. A back-to-back trip departing exactly when the previous one lands is found first, so it correctly yields "in-flight". A zero-length trip (departure_time == arrival_time) is handled by the same logic.
Complexity
With N trips and M queries:
- Time: O(N log N) for the sort plus O(log N) per query, so O(N log N + M log N).
- Space: O(N) for the sorted copy and departure array, plus O(M) for the output.
Reference solution
from bisect import bisect_right
from typing import Dict, List, Optional
def locate_traveler(trips: List[Dict], timestamps: List[int]) -> List[Optional[str]]:
ordered = sorted(trips, key=lambda trip: trip['departure_time'])
starts = [trip['departure_time'] for trip in ordered]
result = []
for t in timestamps:
idx = bisect_right(starts, t) - 1
if idx < 0:
result.append(None)
continue
trip = ordered[idx]
if t <= trip['arrival_time']:
result.append('in-flight')
else:
result.append(trip['arrival_airport'])
return result