Find a User's Airport at a Given Time from Flight Records
RampSoftware Engineer · Technical ScreenLast 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:
- If the
query_timefalls exactly within a flight interval[departure_time, arrival_time], the user is considered to be 'in-flight'. - If the
query_timeis between two flights, the user is at thearrival_airportof the previous flight. - If the
query_timeis before the first flight, the user's location is unknown (return null). - A flight
Bstarts after flightAifB.departure_time >= A.arrival_time.
Function Signature
def find_user_location(flights, queries):
Constraints
flightsare not necessarily sorted by time.query_timeis 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?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.