Reconstruct route from unordered travel tickets
Problem Description
You are provided with a list of directed travel tickets, where each ticket is represented as a pair of city names [from, to] (e.g., ["New York", "London"]). These tickets collectively form exactly one valid trip that visits cities in a single, unbroken chain without any branching.
Key properties of the trip:
- Every city involved in the trip has at most one outgoing ticket and at most one incoming ticket.
- All given tickets are part of this single, continuous trip.
- The starting city of the trip is not explicitly provided.
Task
Your goal is to reconstruct and return the full ordered itinerary as a list of city names. For example, if the input tickets are [["A","B"],["B","C"]], the expected output is ["A","B","C"].
Constraints
- The number of tickets (
tickets.length) will be between 1 and 200,000 (inclusive). - City names are non-empty strings.
- The input is guaranteed to form one valid, non-branching chain.
Output
Return a list of city names representing the reconstructed route. The length of this list should be tickets.length + 1.
AI interview prep
Core dilemma
Efficiently reconstructing a unique linear path from an unordered set of directed edges, specifically identifying the unique start node without explicit information, under a significant scale constraint (N=2e5).
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Reconstruct route from unordered travel tickets
Problem Description
You are provided with a list of directed travel tickets, where each ticket is represented as a pair of city names [from, to] (e.g., ["New York", "London"]). These tickets collectively form exactly one valid trip that visits cities in a single, unbroken chain without any branching.
Key properties of the trip:
- Every city involved in the trip has at most one outgoing ticket and at most one incoming ticket.
- All given tickets are part of this single, continuous trip.
- The starting city of the trip is not explicitly provided.
Task
Your goal is to reconstruct and return the full ordered itinerary as a list of city names. For example, if the input tickets are [["A","B"],["B","C"]], the expected output is ["A","B","C"].
Constraints
- The number of tickets (
tickets.length) will be between 1 and 200,000 (inclusive). - City names are non-empty strings.
- The input is guaranteed to form one valid, non-branching chain.
Output
Return a list of city names representing the reconstructed route. The length of this list should be tickets.length + 1.