Find a Canonical Shortest Round Trip Through Flights
ExpediaSoftware Engineer · OnsiteLast updated
Find the shortest directed round trip starting and ending at a given airport, breaking ties lexicographically.
Problem Statement
You are given a list of directed flights, where each flight is represented as a pair of airport codes [origin, destination], and a start_airport. A round trip is a sequence of airports starting at start_airport, following available directed flights, and returning to start_airport.
Your task is to find a round trip that:
- Uses the minimum number of flights (edges).
- If multiple round trips tie for the minimum number of flights, choose the one that is lexicographically smallest as a sequence of airport codes.
If no round trip is possible, return an empty list [].
Note:
- A flight directly from
start_airporttostart_airport(a self-loop) is valid and counts as a round trip of length 1 (i.e.,[start_airport, start_airport]). - Flights may contain duplicate pairs. A flight can only be traversed in its specified direction.
Function Signature
def find_canonical_round_trip(flights: list[list[str]], start_airport: str) -> list[str]:
pass
Constraints
1 <= len(flights) <= 20,000- Each flight contains exactly two strings:
[origin, destination] - Each airport code is a string of 3 uppercase English letters (e.g.,
"JFK","LAX") start_airportis a 3-letter uppercase English string
Examples
Example 1
flights = [
["JFK", "ORD"],
["ORD", "LAX"],
["LAX", "JFK"],
["JFK", "DFW"],
["DFW", "LAX"]
]
start_airport = "JFK"
# Output: ["JFK", "DFW", "LAX", "JFK"]
Explanation: There are two round trips of length 3 hops:
["JFK", "ORD", "LAX", "JFK"]["JFK", "DFW", "LAX", "JFK"]Both have 3 hops. Comparing them lexicographically,"DFW" < "ORD", so["JFK", "DFW", "LAX", "JFK"]is returned.
Example 2
flights = [
["SFO", "SEA"],
["SEA", "SFO"],
["SFO", "DEN"],
["DEN", "SFO"],
["SFO", "SFO"]
]
start_airport = "SFO"
# Output: ["SFO", "SFO"]
Explanation: The self-loop ["SFO", "SFO"] requires only 1 hop, which is strictly shorter than any 2-hop route.
Example 3
flights = [
["MIA", "ATL"],
["ATL", "BOS"]
]
start_airport = "MIA"
# Output: []
Explanation: It is impossible to return to "MIA", so return [].
Hints
Hint 1
Because each flight has a uniform weight of 1, finding the minimum number of hops corresponds to finding the shortest path in an unweighted directed graph, which can be solved with Breadth-First Search (BFS).
Hint 2
To handle the round trip, search for paths from the neighbors of the start airport back to the start airport, or initialize BFS distances so that the starting airport is not marked visited at step 0.
Hint 3
Be cautious with lexicographical tie-breaking: visiting neighbors in sorted order during standard BFS does not always guarantee the globally lexicographically smallest path across all branches unless paths are compared level-by-level.
Follow-up questions
- How would your approach change if each flight had a positive ticket price and you needed the cheapest round trip instead of the fewest hops?
- If the graph were dynamic and flights were added or canceled in real time, how would you maintain the canonical shortest round trip efficiently?
- How does the time and space complexity change if you need to return all tied canonical shortest round trips rather than just one?
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.