AirMap Route Finder: List Every Simple Flight Route Between Two Airports
Quick Overview
Design a directed flight network class that adds one-way connections, checks for direct flights, and lists or counts every simple route between two airports in lexicographic order. It tests graph modeling, backtracking search with a visited set, and reasoning about exponential output size.
AirMap Route Finder: List Every Simple Flight Route Between Two Airports
Company: Bloomberg
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design an AirMap that models a network of one-way flights between airports. Each airport is identified by a short code such as `"JFK"` or `"LAX"`, and each connection is a directed edge from one airport to another.
The map must be able to enumerate every possible route between two airports, no matter how many hops it takes. A route is the ordered list of airport codes visited, including both the origin and the destination. A route is simple: it never visits the same airport twice.
Results are returned in lexicographic order so that the output is deterministic. Duplicate connections and self loops are ignored, and querying an unknown airport yields an empty result.
Implement the following methods:
- `addConnection(start: str, destination: str)`: add a one-way flight from `start` to `destination`. Duplicate connections and self loops are ignored.
- `getAllRoutes(start: str, destination: str) -> List[List[str]]`: return all simple routes from `start` to `destination`, each as an ordered list of airport codes including both endpoints, sorted in lexicographic order. Return an empty list if no route exists or either airport is unknown.
- `hasConnection(start: str, destination: str) -> bool`: return `True` if a direct one-way flight from `start` to `destination` exists.
- `countRoutes(start: str, destination: str) -> int`: return the number of distinct simple routes from `start` to `destination`.
```hint Avoiding repeats
Build each route one airport at a time and undo the last step when you back out. Keep track of which airports are already on the current route.
```
```hint Order
Pay attention to the order in which you try the next airport. Chosen well, it makes the routes come out already sorted.
```
### Examples
Example 1:
```text
addConnection("JFK", "LAX")
addConnection("JFK", "ORD")
addConnection("ORD", "LAX")
addConnection("ORD", "DEN")
addConnection("DEN", "LAX")
addConnection("JFK", "LAX") (duplicate, ignored)
addConnection("SFO", "SFO") (self loop, ignored)
getAllRoutes("JFK", "LAX") -> [["JFK", "LAX"], ["JFK", "ORD", "DEN", "LAX"], ["JFK", "ORD", "LAX"]]
countRoutes("JFK", "LAX") -> 3
hasConnection("JFK", "LAX") -> True
hasConnection("LAX", "JFK") -> False
hasConnection("SFO", "SFO") -> False
getAllRoutes("LAX", "JFK") -> []
```
There are three simple routes from JFK to LAX. Lexicographic order compares routes airport by airport: "LAX" sorts before "ORD" at the second position, so the direct flight comes first, and at the third position "DEN" sorts before "LAX", so the longer route through Denver comes before JFK, ORD, LAX. LAX has no outgoing flights, so there is no route back to JFK.
Example 2:
```text
addConnection("AAA", "BBB")
addConnection("BBB", "CCC")
addConnection("CCC", "AAA")
getAllRoutes("AAA", "CCC") -> [["AAA", "BBB", "CCC"]]
getAllRoutes("CCC", "BBB") -> [["CCC", "AAA", "BBB"]]
getAllRoutes("AAA", "AAA") -> [["AAA"]]
getAllRoutes("AAA", "ZZZ") -> []
```
The three flights form a cycle, and a simple route cannot go around it more than once, so each query has one route. A route from an airport to itself is the single-airport route, because any longer one would repeat the airport. ZZZ is unknown, so the result is empty.
### Constraints & Assumptions
- Airport codes are non-empty strings of uppercase letters, at most 10 characters long.
- At most `10^4` calls to `addConnection`.
- At most `10^2` calls to `getAllRoutes`.
- Duplicate connections and self loops (`start == destination`) are ignored by `addConnection`.
- A route never visits the same airport twice.
- Routes are returned in lexicographic order.
### Clarifying Questions to Ask
- Does an airport that has only appeared in an ignored self loop count as known?
- Is lexicographic order compared airport code by airport code, or on the route joined into one string?
- How many routes can a query return in practice, and should `countRoutes` avoid building every route in memory?
### What a Strong Answer Covers
- A graph representation that ignores duplicates and self loops and answers direct-flight checks quickly.
- A search that never repeats an airport on the current route and restores its state when it backs out.
- How the routes end up in lexicographic order, and why that order is unique.
- Special cases: unknown airports, a start equal to the destination, and airports with no outgoing flights.
- An honest complexity analysis, including why enumeration can be exponential and what `countRoutes` costs.
### Follow-up Questions
- If the network has no cycles, how would you count routes without enumerating them?
- How would you return only routes with at most a given number of stops?
- How would you support removing a connection?
- Each flight now has a price. How would you find the cheapest route between two airports?
Overview: Design a directed flight network class that adds one-way connections, checks for direct flights, and lists or counts every simple route between two airports in lexicographic order. It tests graph modeling, backtracking search with a visited set, and reasoning about exponential output size.
Design an AirMap that models a network of one-way flights between airports. Each airport is identified by a short code such as "JFK" or "LAX", and each connection is a directed edge from one airport to another.
The map must be able to enumerate every possible route between two airports, no matter how many hops it takes. A route is the ordered list of airport codes visited, including both the origin and the destination. A route is simple: it never visits the same airport twice.
Results are returned in lexicographic order so that the output is deterministic. Duplicate connections and self loops are ignored, and querying an unknown airport yields an empty result.
Implement the following methods:
addConnection(start: str, destination: str)
: add a one-way flight from
start
to
destination
. Duplicate connections and self loops are ignored.
getAllRoutes(start: str, destination: str) -> List[List[str]]
: return all simple routes from
start
to
destination
, each as an ordered list of airport codes including both endpoints, sorted in lexicographic order. Return an empty list if no route exists or either airport is unknown.
hasConnection(start: str, destination: str) -> bool
: return
True
if a direct one-way flight from
start
to
destination
exists.
countRoutes(start: str, destination: str) -> int
: return the number of distinct simple routes from
start
to
destination
.
There are three simple routes from JFK to LAX. Lexicographic order compares routes airport by airport: "LAX" sorts before "ORD" at the second position, so the direct flight comes first, and at the third position "DEN" sorts before "LAX", so the longer route through Denver comes before JFK, ORD, LAX. LAX has no outgoing flights, so there is no route back to JFK.
The three flights form a cycle, and a simple route cannot go around it more than once, so each query has one route. A route from an airport to itself is the single-airport route, because any longer one would repeat the airport. ZZZ is unknown, so the result is empty.
Constraints & Assumptions
Airport codes are non-empty strings of uppercase letters, at most 10 characters long.
At most
10^4
calls to
addConnection
.
At most
10^2
calls to
getAllRoutes
.
Duplicate connections and self loops (
start == destination
) are ignored by
addConnection
.
A route never visits the same airport twice.
Routes are returned in lexicographic order.
Clarifying Questions to Ask Guidance
Does an airport that has only appeared in an ignored self loop count as known?
Is lexicographic order compared airport code by airport code, or on the route joined into one string?
How many routes can a query return in practice, and should
countRoutes
avoid building every route in memory?
What a Strong Answer Covers Guidance
A graph representation that ignores duplicates and self loops and answers direct-flight checks quickly.
A search that never repeats an airport on the current route and restores its state when it backs out.
How the routes end up in lexicographic order, and why that order is unique.
Special cases: unknown airports, a start equal to the destination, and airports with no outgoing flights.
An honest complexity analysis, including why enumeration can be exponential and what
countRoutes
costs.
Follow-up Questions Guidance
If the network has no cycles, how would you count routes without enumerating them?
How would you return only routes with at most a given number of stops?
How would you support removing a connection?
Each flight now has a price. How would you find the cheapest route between two airports?