Find User Airport at a Time
Company: Ramp
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates the ability to model and query time-interval flight data for multiple users, testing skills in data structures, sorting and search algorithms, interval semantics, and handling temporal edge cases and performance trade-offs.
Constraints
- 0 <= len(flights), len(queries) <= 200000
- Each flight has `departure_time < arrival_time`
- For the same user, flights do not overlap; after sorting by departure time, a flight's arrival time is less than or equal to the next flight's departure time
- The input flight records are not guaranteed to be sorted
Examples
Input: ([('u1','LAX','JFK',15,20), ('u2','SEA','DEN',5,9), ('u1','SFO','LAX',10,12), ('u1','JFK','BOS',25,26)], [('u1',9), ('u1',10), ('u1',12), ('u1',14), ('u1',15), ('u1',20), ('u1',24), ('u1',30), ('u2',7), ('u2',9)])
Expected Output: ['SFO', '', 'LAX', 'LAX', '', 'JFK', 'JFK', 'BOS', '', 'DEN']
Explanation: After sorting u1's flights, the queries cover before the first flight, during flights, exact arrival times, between flights, and after the final flight. For u2, time 7 is in the air and time 9 is exactly at arrival.
Input: ([('a','ORD','MIA',100,130)], [('a',99), ('a',100), ('a',130), ('a',200), ('b',50)])
Expected Output: ['ORD', '', 'MIA', 'MIA', '']
Explanation: A single flight shows the boundary behavior: before departure the user is at ORD, at departure they are in the air, at arrival and after arrival they are at MIA. User b has no records, so the answer is an empty string.
Input: ([], [('x',1), ('x',10)])
Expected Output: ['', '']
Explanation: With no flight records at all, every query returns an empty string.
Hints
- Group flight records by user first, then sort each user's flights by departure time.
- For each query, binary search for the last flight whose departure time is less than or equal to the query time.