Reconstruct Itinerary (Graph & Stack)

Data Structures
Hard
Microsoft
102.3K views

Given a list of airline tickets, reconstruct the itinerary in order, starting from 'JFK'. If multiple valid itineraries exist, return the one that has the smallest lexical order. Use DFS/Backtracking on a graph.

Why Interviewers Ask This

Microsoft asks this to evaluate a candidate's mastery of graph traversal algorithms, specifically Hierholzer's algorithm or DFS with backtracking. They assess the ability to handle complex constraints like lexical ordering while ensuring every ticket is used exactly once. It tests whether you can model real-world routing problems as directed graphs and manage state efficiently without getting stuck in dead ends.

How to Answer This Question

1. Clarify requirements: Confirm if all tickets must be used and that 'JFK' is always the start. 2. Model the graph: Explain building an adjacency list where destinations are stored in a min-heap or sorted list to ensure lexical order during traversal. 3. Choose the strategy: Propose using Depth First Search (DFS) with a post-order processing step, noting that adding nodes after recursion ensures we don't get stuck prematurely. 4. Handle edge cases: Discuss how to manage multiple edges between the same airports and what happens if a path becomes invalid. 5. Analyze complexity: Briefly explain time complexity O(E log E) due to sorting and space complexity O(E) for the graph and recursion stack.

Key Points to Cover

  • Explicitly mention using a Min-Heap or Sorted List to handle lexical ordering automatically
  • Explain the critical difference between pre-order and post-order node addition in DFS
  • Demonstrate understanding of Eulerian Path properties where every edge must be traversed exactly once
  • Clarify how to handle duplicate tickets by removing edges rather than just marking visited nodes
  • State the Time Complexity clearly as O(E log E) due to the sorting requirement

Sample Answer

To solve the Reconstruct Itinerary problem, I first visualize the tickets as a directed graph where each airport is a node and a ticket represents a directed edge. Since we need the smallest lexical itinerary, I will store the neighbors for each airport in a sorted data structure, like a priority queue or a sorted list, so we always visit the lexicographically smallest destination first. My approach uses Depth First Search with backtracking. The key insight is that we cannot simply add a node to our result when we first visit it; we might reach a dead end before using all tickets. Instead, I perform a DFS from JFK. For each current airport, I iterate through its available destinations in lexical order. If a destination exists, I remove that specific ticket (edge) and recursively visit the next airport. Crucially, I only add the current airport to my final itinerary list after the recursive call returns. This post-order traversal ensures that we have exhausted all possible paths from a node before committing it to the route. Once the recursion unwinds completely, the list will contain the correct itinerary in reverse order, which I then reverse to get the final answer. This handles the Eulerian path requirement naturally. Regarding complexity, sorting takes O(E log E), and the DFS visits each edge once, making the total time complexity efficient enough for large inputs.

Common Mistakes to Avoid

  • Using a simple visited set instead of removing edges, which fails when multiple tickets exist between the same cities
  • Adding nodes to the result list immediately upon entry (pre-order), causing the algorithm to terminate at a dead end prematurely
  • Failing to sort the adjacency list, leading to incorrect results when multiple valid itineraries exist
  • Not reversing the final result list because the post-order DFS generates the itinerary in reverse chronological order

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 65 Microsoft questions