Time spent at the airport
Build AirportLog, which records passengers coming into an airport and leaving it, and answers how long they stayed. Times are whole minutes.
AirportLog() starts with nobody inside and no visits recorded.
arrive(passenger, time) records that passenger came in at time.
depart(passenger, time) records that passenger left at time. That ends one visit, which lasted time minus the minute they came in.
timeSpent(passenger) returns the minutes passenger has spent in the airport over all their finished visits, or 0 if they have finished none.
averageStay() returns the average length of every finished visit so far, by every passenger, or 0 if no visit has finished.
inside() returns how many passengers are in the airport now.
The calls come in time order: no call has an earlier time than the call before it. arrive is only called for a passenger who is not inside, and depart only for one who is. A passenger may come and go many times.
- Input
["AirportLog","arrive","arrive","inside","depart","timeSpent","averageStay","arrive","depart","depart","timeSpent","timeSpent","averageStay","inside"]
[[],[7,10],[3,15],[],[7,70],[7],[],[7,100],[3,115],[7,150],[7],[3],[],[]]- Output
[null,null,null,2,null,60,60.0,null,null,null,110,100,70.0,0]
Passenger 7 stays 60 minutes, then 50 more: 110 in all. Passenger 3 stays 100. The three finished visits average (60 + 100 + 50) / 3 = 70.
- Input
["AirportLog","averageStay","timeSpent","arrive","depart","averageStay","inside"]
[[],[],[1],[1,5],[1,5],[],[]]- Output
[null,0.0,0,null,null,0.0,0]
A visit that ends the minute it starts lasts 0 minutes, and it still counts as a finished visit.
Limits
1 <= passenger <= 1000000 <= time <= 1000000000times never go down from one call to the nextat most 20000 calls after the constructor
Hints
Hint 1
Remember when each passenger who is inside came in: a map from passenger to arrival time.
Hint 2
Keep running totals as visits end (each passenger's minutes, all the minutes, the number of finished visits), so every call is O(1).
Where this was reported
- Uber, Summer intern on-campus interview, 2023 (GeeksforGeeks)
Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.
Run your code to check it on the examples. Submit runs every hidden test.
