Implement cheapest itinerary with date filters
Company: Circle
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates proficiency in graph algorithms and constrained pathfinding, including handling date filters for initial departures, time-ordered connections, cycle avoidance, and quantitative complexity analysis.
Constraints
- `0 <= len(flights) <= 200000`
- Each flight is `(id, source, destination, departure_time, arrival_time, price)` with unique `id`
- `0 <= departure_time < arrival_time <= 10^9` and `1 <= price <= 10^6`
- `start_date <= end_date`
Examples
Input: ([(101, 1, 2, 10, 12, 100), (102, 2, 3, 13, 15, 80), (103, 1, 3, 11, 14, 300), (104, 2, 3, 12, 16, 60)], 1, 3, 9, 11)
Expected Output: (160, [101, 104])
Explanation: Flight 101 can start within the date window, and flight 104 departs exactly when flight 101 arrives, which is allowed. Total cost is 100 + 60 = 160, cheaper than the direct flight 103.
Input: ([(201, 1, 2, 5, 7, 50), (202, 1, 3, 8, 10, 200), (203, 2, 3, 8, 9, 40)], 1, 3, 6, 8)
Expected Output: (200, [202])
Explanation: Flight 201 is cheaper but cannot be the first leg because it departs before `start_date`. Therefore the only valid itinerary is the direct flight 202.
Hints
- Sort flights by departure time and process them from earliest to latest.
- For each airport, keep track of the cheapest itinerary that has already arrived there by the current departure time.