Code RoomLexicographically smallest itinerary
HardPrep Room Coding #1076

Lexicographically smallest itinerary

CodingAlgorithms & data structuresSenior–Staff~35 min

You have a list of one-way flight tickets [from, to] (airport codes). Reconstruct an itinerary that uses every ticket exactly once and starts at 'JFK'. It is guaranteed at least one valid itinerary exists. If multiple are possible, return the one that is lexicographically smallest when read as a list of airport codes. Return the itinerary as a list of airport codes in visiting order.

Implement
reconstruct_itinerary(tickets: list[list[str]]) → list[str]
Examples
in[[["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]]out["JFK","MUC","LHR","SFO","SJC"]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 35 min
InputExpectedGot
[[["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]]["JFK","MUC","LHR","SFO","SJC"]not run yetsample