Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
All Routes From Origin to Destination
00:00
5 left

All Routes From Origin to Destination

MediumPython

Problem

Hopper's flight search needs to display every possible itinerary between two airports using available directed flight legs. Given a set of flight connections, return all routes from an origin to a destination without visiting any airport more than once.

Formal Specification

Implement find_flight_routes(flights, origin, destination), where flights is a list of two-element lists [from_airport, to_airport]. Each connection is directed. Return a list of routes, where each route is a list of airport codes beginning with origin and ending with destination.

Return routes in the depth-first traversal order induced by the input connection order. If origin == destination, return a route containing only that airport. You may assume there are no duplicate connections. Routes must not contain cycles.

Example 1:

Input: flights = [["YUL", "YYZ"], ["YUL", "BOS"], ["YYZ", "LAX"], ["BOS", "LAX"]], origin = "YUL", destination = "LAX"
Output: [["YUL", "YYZ", "LAX"], ["YUL", "BOS", "LAX"]]

Both one-stop itineraries connect YUL to LAX.

Example 2:

Input: flights = [["YUL", "YYZ"], ["YYZ", "YUL"], ["YYZ", "LAX"]], origin = "YUL", destination = "LAX"
Output: [["YUL", "YYZ", "LAX"]]

The YUL to YYZ to YUL cycle is excluded.

Constraints

  • 0 <= len(flights) <= 10^4
  • Airport codes are non-empty strings
  • Each connection contains exactly two airport codes
  • There are no duplicate directed connections
  • The number of valid routes is manageable for the returned output
  • A route cannot visit an airport more than once

Function Signature

def find_flight_routes(flights, origin, destination):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.