Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Advanced Graphs

Reconstruct Itinerary

HARD✓ CHIP-TIMEDLC #332 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: Rebuild a single trip from JFK that uses every ticket in the pile exactly once. When more than one full itinerary is possible, the one that reads smallest airport-code-first wins.

A pile of one-way airline tickets arrives, each naming a departure airport and an arrival airport. Every one of those tickets has to be used exactly once, the trip has to start at JFK, and it has to use up the entire pile.

More than one itinerary can satisfy that — the tie gets broken by reading order: whichever valid full itinerary comes first alphabetically, city code by city code, is the one to hand back.

This input always admits at least one itinerary that uses every ticket, so the only work is finding the lexicographically smallest one, not proving one exists.

  • ticket count is small, at most a few hundred
  • airport codes are three uppercase letters
  • duplicate tickets between the same pair of airports are allowed
  • trip always starts at JFK and must use every ticket exactly once
  • tie-break: the itinerary that sorts smallest airport-code-first wins
EX 01
tickets = [["JFK", "AAA"], ["AAA", "BBB"], ["BBB", "CCC"]]
→ ["JFK", "AAA", "BBB", "CCC"]
straight chain, no branching
EX 02
tickets = [["JFK", "ABC"], ["JFK", "DEF"], ["DEF", "JFK"]]
→ ["JFK", "DEF", "JFK", "ABC"]
greedy JFK->ABC is a dead end; must detour through DEF first
EX 03
tickets = [["JFK", "AAA"], ["AAA", "JFK"], ["JFK", "AAA"]]
→ ["JFK", "AAA", "JFK", "AAA"]
duplicate ticket used twice

The hintstake only what you need

HINT 1 THE NUDGE

This is a path that must consume every edge of a graph exactly once — not a shortest path, and not a search for any valid order, but the one specific order that sorts first.

HINT 2 THE STRUCTURE

Greedily taking the alphabetically smallest next city can strand you at a dead end with unused tickets still in hand. What lets a route recover from a wrong early choice?

HINT 3 ONE STEP FROM THE ANSWER

Hierholzer's trick: recurse into the smallest unused destination first, and only append a city to the itinerary once it has no departures left. That post-order, reversed, is the answer — dead ends naturally settle to the end.

Coach's boardthe pattern, step by step

THE DETOUR THAT WORKSPATTERN · HIERHOLZER'S ALGORITHMtickets: JFK→ABC, JFK→DEF, DEF→JFK
STACK
— empty —
STEP 1

Sort each city's destinations so the smallest lexical option is tried first. Hierholzer's DFS starts at JFK.

STEP 1 / 8 · ← → WORK TOO

The splitstwo paces, two languages

grind/reconstruct-itinerary.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def findItinerary(self, tickets: List[List[str]]) -> List[str]:
        graph = collections.defaultdict(list)
        for src, dst in sorted(tickets, reverse=True):
            graph[src].append(dst)
        route = []

        def visit(city):
            while graph[city]:
                visit(graph[city].pop())
            route.append(city)

        visit("JFK")
        return route[::-1]
TIME O(E·LOG E)SPACE O(E)PYTHON · RACE PACE · 14 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED