Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Graph-Based Problem Solution
00:00
5 left

Graph-Based Problem Solution

EasyPython

Problem

Shopee Express operates transfer routes between numbered sorting hubs. Each route moves a parcel directly from one hub to another and counts as one transfer. Given the directed routes, return the minimum number of transfers needed to move a parcel from source to destination. Return -1 if the destination is unreachable.

Formal Specification

Implement minimum_transfers(n, routes, source, destination), where n is the number of hubs labeled 0 through n - 1, and routes is a list of directed pairs [u, v] indicating a route from hub u to hub v. Return an integer representing the minimum number of routes on a valid path from source to destination.

Routes may be repeated, and the graph may contain cycles. A hub can be visited at most once during the search.

Constraints

  • 1 <= n <= 10^5
  • 0 <= routes.length <= 2 * 10^5
  • Each route contains exactly two integers [u, v]
  • 0 <= u, v < n
  • 0 <= source, destination < n
  • Routes are directed and may be repeated

Function Signature

def minimum_transfers(n, routes, source, destination):
Interviewer

Your question is Graph-Based Problem Solution. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.