You are given the array paths
, where paths[i] = [cityAi, cityBi]
means there exists a direct path going from cityAi
to cityBi
. Return the destination city, that is, the city without any path outgoing to another city.
It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.
Example 1:
Input: paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]] Output: "Sao Paulo" Explanation: Starting at "London" city you will reach "Sao Paulo" city which is the destination city. Your trip consist of: "London" -> "New York" -> "Lima" -> "Sao Paulo".
Example 2:
Input: paths = [["B","C"],["D","B"],["C","A"]] Output: "A" Explanation: All possible trips are: "D" -> "B" -> "C" -> "A". "B" -> "C" -> "A". "C" -> "A". "A". Clearly the destination city is "A".
Example 3:
Input: paths = [["A","Z"]] Output: "Z"
Constraints:
1 <= paths.length <= 100
paths[i].length == 2
1 <= cityAi.length, cityBi.length <= 10
cityAi != cityBi
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the destination city involves checking every possible city to see if it's the final one. We'll start with the first city in each route and eliminate cities that are departure points.
Here's how the algorithm would work step-by-step:
def find_destination_city(routes):
starting_cities = set()
ending_cities = set()
for route in routes:
starting_city = route[0]
destination_city = route[1]
starting_cities.add(starting_city)
ending_cities.add(destination_city)
# The set of possible destinations has been identified.
for possible_destination in ending_cities:
# Check if city is ONLY in the ending cities list.
if possible_destination not in starting_cities:
# If the ending city is never a starting city, done!
return possible_destination
return None
The problem gives a list of routes, where each route goes from one city to another. The goal is to find the city that is the final destination, meaning it's reached but never departed from. The key idea is to identify cities that are only ever arrived at, and never listed as a starting point.
Here's how the algorithm would work step-by-step:
def find_destination_city(routes):
starting_cities = set()
# Collect all departure cities
for route in routes:
starting_cities.add(route[0])
for route in routes:
destination_city = route[1]
# Check if destination is also a start
if destination_city not in starting_cities:
# If not, it's the final destination
return destination_city
return None # Should not normally reach here
Case | How to Handle |
---|---|
Empty input list of paths | Return an empty string or null, indicating no destination city is found, or throw an exception based on problem requirements. |
Input list containing only one path | The destination city is simply the second city in the single path. |
All paths form a single linear route (A -> B -> C -> D) | The last city in the chain is the destination city; solution should traverse the paths to identify it. |
Paths contain cycles (A -> B -> C -> A) | Cycles should not affect the determination of the destination city, as the destination will not have an outgoing path even within a cycle. |
Multiple possible destination cities (invalid input according to problem statement, but handle gracefully) | Return one of the destination cities, throw an error, or return an empty string as the problem states a single destination should exist. |
City names contain special characters or are very long strings | String operations should handle special characters and long strings efficiently without causing errors (check for memory limits with extremely large strings). |
Large input list of paths (performance considerations) | Using a hash set or dictionary for efficient lookups ensures the algorithm scales well with a large number of paths (O(N) time complexity). |
City names are case-sensitive (e.g., 'New York' vs 'new york') | The solution should either be case-sensitive, or a pre-processing step can be added to convert all city names to a uniform case. |