Reconstruct Itinerary
Given a list of flight tickets, reconstruct the itinerary in order. The itinerary must begin with 'JFK'. If there are multiple valid itineraries, you should return the one that has the smallest lexical order. Use DFS and sorting.
Why Interviewers Ask This
Stripe evaluates candidates on graph traversal and state management under constraints. This question tests the ability to model real-world flight data as a directed graph, handle complex backtracking logic when dead-ends occur, and implement Hierholzer's algorithm efficiently. It specifically assesses lexical ordering requirements and the candidate's proficiency with DFS in non-trivial scenarios.
How to Answer This Question
Key Points to Cover
- Explicitly mention using a HashMap with sorted lists for the adjacency structure
- Demonstrate understanding of post-order DFS traversal for Eulerian path reconstruction
- Explain how reversing the result stack yields the correct forward itinerary
- Address the specific requirement of finding the lexicographically smallest path
- Reference handling of disconnected components or dead-end scenarios gracefully
Sample Answer
Common Mistakes to Avoid
- Attempting standard pre-order DFS which fails to handle dead-ends and produces invalid itineraries
- Forgetting to sort the destination lists, leading to incorrect lexical ordering results
- Using recursion without managing the visited state properly, causing infinite loops or missed edges
- Neglecting to reverse the final result stack, outputting 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.