Destination City - Problem

You are given an 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.

Input & Output

Example 1 — Basic Chain
$ Input: paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
Output: "Sao Paulo"
💡 Note: Following the path: London → New York → Lima → Sao Paulo. Sao Paulo has no outgoing path, so it's the destination.
Example 2 — Simple Path
$ Input: paths = [["B","C"],["D","B"],["C","A"]]
Output: "A"
💡 Note: The path is D → B → C → A. City A appears as a destination but never as a source.
Example 3 — Single Path
$ Input: paths = [["A","Z"]]
Output: "Z"
💡 Note: Only one path A → Z, so Z is the destination city with no outgoing paths.

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.

Visualization

Tap to expand
Destination City Problem OverviewInput: Path Array["London","New York"]["New York","Lima"]["Lima","Sao Paulo"]Path Chain VisualizationLONNYCLIMSAOLondon → New York → Lima → Sao PauloKey InsightThe destination city appears as arrival but never as departureSources: {London, New York, Lima}Destinations: {New York, Lima, Sao Paulo}Answer: Sao Paulo (not in sources)
Understanding the Visualization
1
Input Paths
Array of city pairs representing directed paths
2
Find Pattern
Destination city has no outgoing path
3
Output
Return the city that only appears as destination
Key Takeaway
🎯 Key Insight: The destination city is the only city that appears as an arrival but never as a departure in any path
Asked in
Google 15 Amazon 12 Apple 8 Facebook 6
31.5K Views
Medium Frequency
~10 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen