Reconstruct an Itinerary Without a Fixed Start
Updated
Problem statement
You are given directed flight tickets, where tickets[i] = [from, to]. Use every ticket exactly once and return the airports in travel order.
A valid itinerary is guaranteed to exist. The starting airport is not fixed:
- If one airport has exactly one more outgoing ticket than incoming tickets, it is the required start.
- Otherwise the tickets form an Eulerian circuit; start at the lexicographically smallest airport that has an outgoing ticket.
When more than one complete itinerary is possible from that start, return the lexicographically smallest airport sequence.
Function
findItinerary(tickets: List<List<String>>) → List<String>Examples
Example 1
tickets = [["SFO","ATL"],["ATL","LAX"],["LAX","SFO"]]return = ["ATL","LAX","SFO","ATL"]The tickets form a circuit, so the lexicographically smallest airport with an outgoing ticket, ATL, is used as the start.
Example 2
tickets = [["A","B"],["A","C"],["B","A"]]return = ["A","B","A","C"]Airport A has one extra outgoing ticket and is the required start. Choosing B before C produces the lexicographically smallest complete path.
Example 3
tickets = [["B","C"],["A","B"]]return = ["A","B","C"]The degree difference identifies A as the only valid start.
Constraints
1 <= tickets.length <= 2 * 10^5tickets[i].length == 2- Every airport name has between
1and10uppercase English letters. - The tickets admit at least one itinerary that uses every ticket exactly once.
More Pinterest Phone Screen questions
- Shortest Pin Path Through TopicsPHONE SCREEN · Seen Nov 2026
- Top K Pins with Insert and QueryPHONE SCREEN · Seen Nov 2026
- Pins Violation Log QueriesPHONE SCREEN · Seen Nov 2026
- Sparse Matrix Addition and MultiplicationPHONE SCREEN · Seen Nov 2026
- Minimum Bus Routes to a DestinationPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delete and Compact a Forest SubtreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Optimal Account BalancingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Aggregate Matching IPv4 Addresses Across ResponsesPHONE SCREEN · Seen Oct 2026
- Escape Room LeaderboardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Shortest Way to Form a Target StringPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Reconstruct ItineraryTripjack · ONSITE INTERVIEWTripjack interview questions
- Find All People with the SecretAmazon · PHONE SCREEN
- Divide Tree NodesGoogle · OA · ONSITE INTERVIEWGoogle interview questions
- Find All People With SecretIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Contact Tracing Through Timed MeetingsHudson River Trading · PHONE SCREENHudson River Trading interview questions
- Mutual-Friend Referral RecommendationsFireworks AI · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Deterministic Secret Santa AssignmentShopify · PHONE SCREENShopify interview questions
- Top-Down Employee Reporting ForestOkta · ONSITE INTERVIEWOkta interview questions
- Aggregate Machine TopologyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions