Katkat

Find a User's Airport at a Given Time from Flight Records

RampSoftware Engineer · Technical Screen

Last updated

This problem tests your ability to manage and query interval-based time-series data, specifically determining state (location) by processing ordered flight events.

Problem Statement

You are given a list of flights for a user and a list of queries. Each flight is represented as an object or dictionary with departure_airport, arrival_airport, departure_time, and arrival_time. A query consists of a user_id and a query_time.

Your task is to determine which airport the user is at for each query_time. Assume the following rules:

  1. If the query_time falls exactly within a flight interval [departure_time, arrival_time], the user is considered to be 'in-flight'.
  2. If the query_time is between two flights, the user is at the arrival_airport of the previous flight.
  3. If the query_time is before the first flight, the user's location is unknown (return null).
  4. A flight B starts after flight A if B.departure_time >= A.arrival_time.

Function Signature

def find_user_location(flights, queries):

Constraints

  • flights are not necessarily sorted by time.
  • query_time is an integer representing a timestamp.
  • Assume a user cannot be on two flights simultaneously.

Example

Input:

flights = [
    {'dep': 'JFK', 'arr': 'LAX', 'start': 100, 'end': 200},
    {'dep': 'LAX', 'arr': 'SFO', 'start': 300, 'end': 400}
]
queries = [150, 250, 350, 450]

Output: ['in-flight', 'LAX', 'in-flight', 'SFO']

Explanation:

  • At 150: Between 100 and 200 (in-flight).
  • At 250: After first flight ends, before second starts (LAX).
  • At 350: Between 300 and 400 (in-flight).
  • At 450: After second flight ends (SFO).

Hints

Hint 1

Sort the flight records by departure time to create a chronological timeline.

Hint 2

For a given query time, use binary search to quickly find the flight that immediately precedes or contains the query time.

Hint 3

Handle the 'in-flight' condition by checking if the query time falls within the start and end boundaries of the flight found by the search.

Follow-up questions

  • How would your solution change if the input included 'ground time' buffers (e.g., minimum time required at an airport before a connection)?
  • What is the time complexity of your solution if we have N flights and M queries, and how can we optimize it for many queries?
  • How would you handle overlapping flight records that indicate a data error or simultaneous travel?