I knew this was an Eulerian path problem pretty quickly, which felt good.
Model the tickets as a directed graph and use Hierholzer's algorithm to find an Eulerian path starting from JFK. To ensure the lexicographically smallest itinerary, sort each airport's destinations in reverse order and use a stack-based DFS, building the itinerary in reverse.
Pro tip: Emphasize that sorting destinations in reverse order and using a stack naturally yields the correct lexical order without extra sorting of the final path. Also, mention that this approach handles edge cases like multiple valid paths and disconnected components.
Recognize that this is finding an Eulerian path in a directed graph where each ticket is an edge. The path must start at JFK and use all edges exactly once.
Use Hierholzer's algorithm for Eulerian path. It efficiently finds a path by following edges until stuck, then backtracking.
Sort each airport's destination list in reverse lexical order. Use a stack (or recursion) to explore, ensuring the smallest lexical path is built.
Build the graph, run DFS, and collect the itinerary in reverse. Test with edge cases like multiple valid paths and cycles.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.