Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reconstruct Itinerary Path

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Reconstruct Itinerary Path. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

MakeMyTrip receives a list of directed itinerary segments, where each segment represents travel from one city to another. Given a starting city, reconstruct a valid route that uses every segment exactly once. If multiple valid routes exist, return the lexicographically smallest route by city code.

Formal Specification

Implement reconstruct_itinerary(itineraries, start). itineraries is a list of pairs [from_city, to_city], and start is the starting city code. Return a list of city codes containing the starting city and one destination for every itinerary segment. The input is guaranteed to contain at least one valid route beginning at start. City codes are uppercase strings.

Constraints

  • 1 <= len(itineraries) <= 10^5
  • Each itinerary segment contains exactly two city codes
  • City codes contain exactly 3 uppercase letters
  • A valid route beginning at start exists
  • Every segment must be used exactly once

Function Signature

def reconstruct_itinerary(itineraries, start):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output