332. Reconstruct Itinerary
Asked at Netflix, Pinterest
Difficulty
Hard
Time
O(E log E)
Space
O(E)
Pattern
Array
Companies
2
Problem
You get a list of flight tickets. Each ticket is a pair [from, to] — the airport you leave and the airport you land at. All these tickets belong to one trip, and the trip starts at "JFK".
Your job is to put all the tickets in one order so that the arrival airport of each ticket is the departure airport of the next one. Every ticket must be used exactly once. The itinerary starts with "JFK", so the returned list has one more airport than the number of tickets.
Sometimes more than one valid order exists. In that case, return the one that is smallest when you read the whole list as a single string (lexicographic order). For example, ["JFK", "LGA"] is smaller than ["JFK", "LGB"] because A comes before B. You may assume at least one valid itinerary always exists.
Tickets are edges in a graph
Constraints
- ●
1 <= tickets.length <= 300 - ●
tickets[i].length == 2 - ●
from_i.length == 3 - ●
to_i.length == 3 - ●
from_i and to_i consist of uppercase English letters - ●
from_i != to_i
Examples
tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]["JFK","MUC","LHR","SFO","SJC"]tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]["JFK","ATL","JFK","SFO","ATL","SFO"]Asked At
2 companies| Company | Difficulty | |
|---|---|---|
| Netflix | Hard | View all Netflix questions → |
| Hard | View all Pinterest questions → |
How to Think About It
Model the problem as a directed multigraph where airports are nodes and tickets are edges.
The problem asks for an Eulerian path starting at JFK that uses every edge exactly once.
Use Hierholzer algorithm: DFS greedily visiting neighbors in sorted order.
Sort adjacency lists so the smallest destination is visited first for lexicographic order.
Backtrack edges only after exhausting all outgoing edges from a node to build the path in reverse.
Optimal Approach
Build an adjacency list mapping each airport to its destinations, sorting destinations lexicographically. Use Hierholzer algorithm to find the Eulerian path: start from JFK, recursively visit the smallest unvisited destination, and push airports to the result stack after exhausting all edges. Since adjacency lists are sorted, the first complete path found is lexicographically smallest. Reverse the result to get the correct itinerary order. Time complexity is O(E log E) for sorting edges and O(E) for the traversal.
Hierholzer: DFS then reverse
What Trips People Up in Real Interviews
Clarify that all tickets must be used exactly once (Eulerian path requirement).
Explain why sorting adjacency lists lexicographically ensures the smallest itinerary.
Discuss Hierholzer algorithm as the standard approach for Eulerian paths.
Mention that using a multiset or list removal handles duplicate tickets correctly.
Address why reversing the result at the end gives the correct order.
Solution Code
O(E log E) timeclass Solution:
def findItinerary(self, tickets: list[list[str]]) -> list[str]:
from collections import defaultdict
graph = defaultdict(list)
for src, dst in sorted(tickets, reverse=True):
graph[src].append(dst)
result = []
def dfs(airport):
while graph[airport]:
dfs(graph[airport].pop())
result.append(airport)
dfs('JFK')
return result[::-1]Frequently Asked Questions
What is the Reconstruct Itinerary problem?
Given a list of airline tickets as pairs [from, to], reconstruct the itinerary in order. All tickets must be used exactly once, and the itinerary must begin with JFK. If multiple valid itineraries exist, return the lexicographically smallest one.
How do you solve Reconstruct Itinerary?
The optimal approach runs in O(E log E) time and O(E) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Reconstruct Itinerary?
Reconstruct Itinerary is asked at Netflix, Pinterest. It is a hard difficulty problem.
What are common mistakes on Reconstruct Itinerary?
- Clarify that all tickets must be used exactly once (Eulerian path requirement).
- Explain why sorting adjacency lists lexicographically ensures the smallest itinerary.
- Discuss Hierholzer algorithm as the standard approach for Eulerian paths.
- Mention that using a multiset or list removal handles duplicate tickets correctly.
- Address why reversing the result at the end gives the correct order.