hardAdvanced GraphsGoogleUber
Flight Itinerary Weaver
A travel agent reconstructs a single itinerary that uses every provided flight ticket exactly once, starting from JFK, preferring lexicographically smaller stops when choices tie.
Given a list of airline tickets as (from, to), return the itinerary that uses all tickets starting from 'JFK'. If multiple orderings exist, return the lexicographically smallest.
Choose approach
JFK
LHR
MUC
cur
SFO
SJC
1/10
Narrationstep 1 of 10
Fly JFK → MUC. Stack: JFK > MUC
Variables
depth=2
next=MUC
How to think about it
- The problem has a guaranteed Eulerian trail starting at JFK.
- Prepending dead-ends to the output builds the trail correctly.
- Priority queue / sorted lists enforce lexicographic order.
Edge cases to catch in interview
- Single ticket -> [from, to].
- Guaranteed solution exists.
Examples
Example 1
JFK,MUC,LHR,SFO,SJC{"tickets":[["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed