File: destination-city/solution.py

Date: 2026-06-06

Time: 16:19

Purpose

This file solves LeetCode 1436 — Destination City. Given a list of directed paths between cities that form a single linear chain (no branches, no cycles), it finds the terminal city — the one that appears only as a destination, never as a source.

It's a standalone solution module following the repo's convention: one Solution class per problem directory, with a corresponding test_solution.py for validation.

Key Components

Solution.destCity(paths: List[List[str]]) -> str

The only method. Takes a list of [source, destination] pairs and returns the city with no outgoing edge.

Contract: paths is non-empty and forms a valid linear chain per the problem's guarantees. Returns a single city name as a string. Raises ValueError on empty input.

Patterns

Set-based membership test: The algorithm builds a set of all source cities, then scans destinations to find one absent from that set. This is the canonical O(n) approach for this problem — it avoids graph construction entirely by exploiting the fact that in a linear chain, exactly one city will never be a departure point.

Early return on match: The for loop returns as soon as it finds the destination city, avoiding unnecessary iteration. Given the problem guarantees, this always fires.

Defensive fallback: The return "" at the end is unreachable under valid input but satisfies the type checker / linter by ensuring all code paths return a value.

Dependencies

Imports: Only typing.List — no external dependencies.

Imported by: destination-city/test_solution.py directly. The massive "Imported By" list in the context is misleading — those are other problems' test files that import their own solution.py, not this one. The cross-referencing tool likely matched on the common from solution import Solution pattern shared across all problem directories.

Flow

1. Guard clause: reject empty paths with ValueError.

2. Build sources — a set comprehension over path[0] for every path. This is O(n) time and space.

3. Iterate over paths again, checking each path[1] (destination) against sources.

4. Return the first destination not in sources.

Total: two passes over paths, O(n) time, O(n) space for the set.

Invariants

Error Handling

Only one explicit error: ValueError for empty input. No handling for malformed path entries (e.g., paths with fewer than 2 elements) or non-string values — these would surface as index errors or unexpected behavior at the caller's level, consistent with the repo's LeetCode-style trust-the-input convention.

Topics to Explore

Beliefs