LeetCode Problem Workspace
Destination City
Find the destination city from a list of paths where each path connects two cities. The city with no outgoing path is the destination.
3
Topics
7
Code langs
3
Related
Practice Focus
Easy · Array scanning plus hash lookup
Answer-first summary
Find the destination city from a list of paths where each path connects two cities. The city with no outgoing path is the destination.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array scanning plus hash lookup
This problem can be solved by scanning through the paths array and using a hash lookup to identify the destination city. The city that does not appear as the starting point of any path is the destination. Efficient scanning with hash tables makes this solution optimal for large input sizes.
Problem Statement
You are given a list of direct paths between cities, represented as an array of pairs. Each pair, paths[i] = [cityAi, cityBi], indicates a direct path from cityAi to cityBi. The goal is to determine which city is the destination city, i.e., the city that has no outgoing paths.
Since the paths form a direct line with no loops, there is guaranteed to be exactly one destination city. You are tasked with finding this destination city efficiently.
Examples
Example 1
Input: paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
Output: "Sao Paulo"
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"
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"
Example details omitted.
Constraints
- 1 <= paths.length <= 100
- paths[i].length == 2
- 1 <= cityAi.length, cityBi.length <= 10
- cityAi != cityBi
- All strings consist of lowercase and uppercase English letters and the space character.
Solution Approach
Scan through the cities
Start by scanning the list of paths. Keep track of all cities that appear as starting points (cityAi) using a set or hash table. This allows you to quickly identify the cities that have outgoing paths.
Find the destination city
The destination city will be the one that is not present in the set of starting cities. You can efficiently check each city in the paths list and return the one that doesn’t have an outgoing path.
Optimize for time and space
The time complexity of this approach is O(n) due to the need to scan through the paths once, and the space complexity is O(n) due to the use of a set or hash table for storing the starting cities.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | O(n) |
| Space | O(n) |
The algorithm scans through all the paths exactly once, and uses a set or hash table to store starting cities, ensuring the time complexity is linear, O(n). The space complexity is also O(n), where n is the number of paths, because the set stores the cities as starting points.
What Interviewers Usually Probe
- Candidate identifies the key insight: finding the city with no outgoing path.
- Candidate demonstrates an understanding of hash table or set usage for efficient lookups.
- Candidate efficiently handles edge cases like single-path graphs or varying city names.
Common Pitfalls or Variants
Common pitfalls
- Mistaking the first city in the list as the destination without considering all paths.
- Overcomplicating the solution by using unnecessary data structures.
- Missing the linear time complexity requirement and attempting inefficient solutions like nested loops.
Follow-up variants
- The problem can be extended to find multiple destination cities in a more complex graph where multiple disconnected lines of paths exist.
- Consider adding a constraint that allows for more than one city to have no outgoing path, turning the problem into a search for all destination cities.
- Variant: What if the paths form a loop? How would the solution adapt to handle circular references?
FAQ
How do you solve the Destination City problem?
By scanning the list of paths and using a hash table to track cities with outgoing paths, you can quickly identify the city without an outgoing path as the destination.
What is the time complexity of the Destination City problem?
The time complexity is O(n), where n is the number of paths, because you scan through the list once and perform constant-time lookups.
What data structures are useful for the Destination City problem?
A set or hash table is ideal for tracking cities with outgoing paths, allowing for efficient identification of the destination city.
How does the pattern apply to other problems?
This problem demonstrates a common approach of scanning an array while using a hash table to quickly identify elements that meet specific conditions, applicable to a variety of array and graph traversal problems.
Can this problem have more than one destination city?
No, the problem guarantees there is exactly one destination city since the paths form a linear graph without loops.
Solution
Solution 1: Hash Table
According to the problem description, the destination city will not appear in any of the $\textit{cityA}$. Therefore, we can first traverse the $\textit{paths}$ and put all $\textit{cityA}$ into a set $\textit{s}$. Then, we traverse the $\textit{paths}$ again to find the $\textit{cityB}$ that is not in $\textit{s}$.
class Solution:
def destCity(self, paths: List[List[str]]) -> str:
s = {a for a, _ in paths}
return next(b for _, b in paths if b not in s)Continue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array scanning plus hash lookup
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward