KatInterview

Locate a Traveler's Airport by Timestamp

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designbinary-searchhash-mapintervals

Last updated

Process a batch of operations that load travel records and answer per-user point-in-time location queries using grouping, sorting and binary search.

Where Was the Traveler?

You are building a small lookup service for travel records. Each record is a trip written as [user_id, airport_code, start_time, end_time]. It means that the user was at airport_code for every moment in the closed interval [start_time, end_time], so both endpoints are included.

The service is driven by a batch of commands. You write a single function that receives two parallel lists, operations and arguments, processes the commands in order, and returns one result per command.

Commands

  • "__init__": the matching entry in arguments is a list with one element, the list of trips. Discard any data loaded earlier and replace it with these trips. Record None as the result.
  • "get_airport": the matching entry in arguments is [user_id, timestamp]. Record the airport code where that user was at that timestamp, or None if the user was not at any airport at that moment (including when the user has no trips at all).
  • Any other operation name must raise a ValueError.

If a "get_airport" command appears before any "__init__", there is no data, so the result is None.

Rules

  • A user is at no more than one airport at any moment.
  • Trips belonging to the same user never overlap in time.
  • Trips may be given in any order, and the same user's trips may be spread throughout the list.
  • A trip with start_time == end_time is valid and covers exactly that one moment.
  • Each trip may arrive as a list or a tuple.

Output

A list with one entry per command, in order: None for "__init__", and an airport code string or None for each "get_airport".

The number of trips can be large, so answering a query should be much faster than scanning every trip.

Constraints

  • len(operations) == len(arguments).
  • user_id and airport_code are strings; start_time, end_time and timestamp are integers.
  • start_time <= end_time for every trip.
  • Trips of the same user do not overlap.
  • A query should run in sub-linear time in the total number of trips.

Examples

Example 1

Input: locate_traveler(['__init__', 'get_airport', 'get_airport', 'get_airport'], [[[['Alice', 'JFK', 10, 20], ['Alice', 'LAX', 30, 40], ['Bob', 'SFO', 15, 25]]], ['Alice', 15], ['Alice', 25], ['Bob', 20]])

Output: [None, 'JFK', None, 'SFO']

Example 2

Input: locate_traveler(['__init__', 'get_airport', 'get_airport'], [[[['u', 'A', 1, 2]]], ['u', 1], ['u', 2]])

Output: [None, 'A', 'A']

Hints

Hint 1

Only the queried user's trips matter, so group all trips by user in a dictionary.

Hint 2

After sorting one user's trips by start time, at most one trip can contain a given timestamp: the last trip that starts at or before it.

Hint 3

Use bisect_right on the list of start times to find that candidate, then check that the timestamp is no later than its end time. Build the start-time lists once per load, not once per query.