KatInterview

Where Is the Traveler? Locate by Timestamp from Trip Logs

Heaps, Sorting & SearchAuto-graded · 19 test cases
MediumAuto-gradedHeaps, Sorting & Searchbinary-searchsortingintervals

Last 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 from
  • arrival_airport: the code of the airport the trip lands at
  • departure_time: an integer timestamp at which the trip begins
  • arrival_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:

  1. If t lies in a trip's closed interval [departure_time, arrival_time] (both endpoints included), the answer is the string "in-flight".
  2. Otherwise, if t is later than the end of some trip and earlier than the start of the next one (or no trip follows), the answer is the arrival_airport of the most recent trip that has already ended.
  3. Otherwise, if t is earlier than the departure of every trip, the answer is None.

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_time is at least the previous trip's arrival_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, or None.
  • 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.