Find the destination city with no outgoing path in an unordered list of routes

Quick Overview

Given a list of direct paths between cities that form a single chain with no loops, return the one city that has no outgoing path. The problem tests reasoning about outgoing edges in a directed graph when the input is unordered, and efficient membership checks on strings.

Find the destination city with no outgoing path in an unordered list of routes

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list `paths`, where `paths[i] = [city_a, city_b]` means there is a direct path going from `city_a` to `city_b`. Return the destination city, that is, the city without any path going out to another city. The paths are guaranteed to form a single line without any loop, so there is exactly one destination city. ### Function Signature ```python def dest_city(paths: list[list[str]]) -> str: ``` ### Rules - Each entry of `paths` is a two-element list `[origin, destination]` describing one directed path. - The entries can appear in any order; they are not listed in travel order. - City names are compared as exact strings. They are case-sensitive and may contain spaces (`"New York"` is one city). - Return the name of the destination city as a string. ### Constraints - `1 <= len(paths) <= 100` - `len(paths[i]) == 2` - `1 <= len(city_a), len(city_b) <= 10` - `city_a != city_b` - All strings consist of lowercase and uppercase English letters and the space character. ### Examples ```text Input: paths = [["London", "New York"], ["New York", "Lima"], ["Lima", "Sao Paulo"]] Output: "Sao Paulo" ``` The path graph is London -> New York -> Lima -> Sao Paulo. Sao Paulo is the only city with no outgoing path. ```text Input: paths = [["B", "C"], ["D", "B"], ["C", "A"]] Output: "A" ``` The paths are given out of order, but they form the line D -> B -> C -> A. B, C and D each start a path, and A does not. ```text Input: paths = [["A", "Z"]] Output: "Z" ``` With a single path, the city it ends at has nothing going out of it.

Overview: Given a list of direct paths between cities that form a single chain with no loops, return the one city that has no outgoing path. The problem tests reasoning about outgoing edges in a directed graph when the input is unordered, and efficient membership checks on strings.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list paths, where paths[i] = [city_a, city_b] means there is a direct path going from city_a to city_b. Return the destination city, that is, the city without any path going out to another city.

The paths are guaranteed to form a single line without any loop, so there is exactly one destination city.

Function Signature

def dest_city(paths: list[list[str]]) -> str:

Rules

  • Each entry of paths is a two-element list [origin, destination] describing one directed path.
  • The entries can appear in any order; they are not listed in travel order.
  • City names are compared as exact strings. They are case-sensitive and may contain spaces ( "New York" is one city).
  • Return the name of the destination city as a string.

Constraints

  • 1 <= len(paths) <= 100
  • len(paths[i]) == 2
  • 1 <= len(city_a), len(city_b) <= 10
  • city_a != city_b
  • All strings consist of lowercase and uppercase English letters and the space character.

Examples

Input: paths = [["London", "New York"], ["New York", "Lima"], ["Lima", "Sao Paulo"]]
Output: "Sao Paulo"

The path graph is London -> New York -> Lima -> Sao Paulo. Sao Paulo is the only city with no outgoing path.

Input: paths = [["B", "C"], ["D", "B"], ["C", "A"]]
Output: "A"

The paths are given out of order, but they form the line D -> B -> C -> A. B, C and D each start a path, and A does not.

Input: paths = [["A", "Z"]]
Output: "Z"

With a single path, the city it ends at has nothing going out of it.