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
Earliest Arrival with Temporary Road Closures
00:00
5 left

Earliest Arrival with Temporary Road Closures

HardPython

Problem

Torc Robotics needs to route an autonomous vehicle through a directed road network while accounting for temporary road closures. Given the vehicle's start time, compute the earliest possible arrival time at a destination. The vehicle may wait at any intersection, and a road can be used only if it remains open for the entire traversal.

Implement earliest_arrival(graph, closures, start, goal, start_time).

Formal Specification

  • graph is a dictionary mapping each node to a list of [neighbor, travel_time] pairs.
  • closures maps a directed edge string, formatted as "u->v", to sorted, non-overlapping closure intervals [open_start, open_end]. Each interval uses half-open semantics: the road is closed at times open_start <= t < open_end.
  • start, goal, and node identifiers are strings.
  • start_time is a nonnegative integer.
  • Return the earliest integer arrival time at goal, or -1 if the destination is unreachable.

A vehicle may depart an edge at any integer time at or after its current arrival time. Waiting is allowed. A traversal [departure, departure + travel_time) must not intersect a closure interval.

Examples

Example 1: graph = {"A":[["B",4],["C",2]],"B":[["D",3]],"C":[["D",6]],"D":[]}, closures = {"A->B":[[3,10]]}, start = "A", goal = "D", start_time = 0
Output: 9
The direct route through B must wait until time 10, arriving at 13. The route through C arrives at 2 + 6 = 8, but the earliest safe route considering the closure is 8.

Example 2: graph = {"A":[["B",5]],"B":[]}, closures = {"A->B":[[0,20]]}, start = "A", goal = "B", start_time = 0
Output: 20
The vehicle waits until the closure ends, then reaches B at time 25. Therefore the correct output is 25.

Constraints

  • 1 <= number of nodes <= 10^5
  • 0 <= number of directed edges <= 2 * 10^5
  • Travel times are positive integers.
  • Closure intervals for each edge are sorted and non-overlapping.

Constraints

  • 1 <= number of nodes <= 10^5
  • 0 <= number of directed edges <= 2 * 10^5
  • 1 <= travel_time <= 10^9
  • 0 <= start_time <= 10^9
  • Closure intervals are sorted and non-overlapping for each directed edge
  • Closure intervals use half-open semantics [open_start, open_end]

Function Signature

def earliest_arrival(graph, closures, start, goal, start_time):
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.