Reconstruct Itinerary
Reported by candidates from PhonePe's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that trips people up in this PhonePe OA, reported in July 2026, is that every ticket must be used exactly once and the route must start at "JFK". That's an Eulerian path in a directed graph, and the lexicographic tiebreak is what makes it feel harder than it is. You're not searching for a path, you're stitching one together. If you blank on the standard trick under the timer, StealthCoder is the invisible safety net that reads the problem and hands you a working solution. But the pattern is short enough to own before you sit down.
The problem
You are given airline tickets represented as [from, to] pairs. Reconstruct the itinerary that uses every ticket exactly once. The itinerary must start at "JFK". If multiple valid itineraries exist, return the lexicographically smallest itinerary when read as a single sequence of airport codes. Function findItinerary(tickets: String[][]) → String[] Examples Example 1 tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]] return = ["JFK","MUC","LHR","SFO","SJC"] There is only one route that uses all tickets from JFK. Example 2 tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]] return = ["JFK","ATL","JFK","SFO","ATL","SFO"] Both choices from JFK can lead to valid routes, but the route beginning with ATL is lexicographically smaller.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is Hierholzer's algorithm. Build an adjacency map from each airport to its destinations, sorted lexicographically (a min-heap or a sorted list you pop from the front). Run a DFS from JFK. At each node, keep taking the smallest unused outgoing ticket. When a node has no tickets left, append it to the result. Reverse the result at the end. The common pitfall is naive backtracking that tries every ordering. It works on the examples and times out on bigger inputs. Another mistake is appending airports on the way down instead of on the way back up, which breaks dead-end handling like the SFO to SJC leg in Example 1. Remember that duplicate tickets are legal, so don't use a set. Complexity is O(E log E) from the sorting. If the live OA scrambles your memory of the post-order reversal, StealthCoder can cover that gap in real time.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Reconstruct Itinerary cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as reconstruct itinerary. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass PhonePe's OA.
PhonePe reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Reconstruct Itinerary FAQ
How hard is Reconstruct Itinerary really?+
It's a LeetCode Hard, but the solution is about fifteen lines once you know Hierholzer's algorithm. The difficulty is recognizing it's an Eulerian path problem. If you've never seen it, it feels brutal. If you have, it's mechanical.
What's the trick for the PhonePe version?+
Sort destinations per airport, DFS from JFK always taking the smallest unused edge, and add an airport to the answer only after it has no tickets left. Then reverse the list. Post-order insertion is what handles dead ends correctly.
Why does plain backtracking fail?+
Backtracking tries permutations of tickets and retraces on dead ends, which blows up exponentially on larger inputs. Hierholzer's visits each edge once, so you get O(E log E) instead. It may pass small samples and then time out on hidden tests.
Which data structure should I use for the adjacency list?+
A hash map from airport to a min-heap works well in Python and Java. You can also sort each list in reverse and pop from the end. Either way, don't use a set, because duplicate tickets between the same airports are allowed.
How do I prepare in 48 hours?+
Write Hierholzer's from memory twice, once recursive and once iterative. Trace Example 2 by hand to see why ATL comes before SFO and how the reversal fixes the order. Then skim other graph problems that use DFS post-order so the pattern sticks.