Katkat

Compute ETA between two map nodes

WaymoSoftware Engineer · Technical Screen

Last updated

This problem tests your ability to implement Dijkstra's algorithm to find the shortest path in a weighted directed graph representing a road network.

Problem Statement

You are given a map represented as a directed graph where nodes are intersections and edges are road segments. Each edge has an associated travel_time representing the number of minutes required to traverse that segment. Given a starting node start_node, an ending node end_node, and an adjacency list graph where graph[u] contains a list of tuples (v, travel_time), implement a function to calculate the minimum travel time from start_node to end_node.

If the destination is unreachable, return -1.

Function Signature

def compute_eta(graph, start_node, end_node) -> int:

Constraints

  • The graph contains N nodes (0 to N-1) and E edges.
  • N is up to 10,000; E is up to 100,000.
  • travel_time is always non-negative (>= 0).

Example 1

Input:

graph = {0: [(1, 5), (2, 1)], 1: [(3, 2)], 2: [(1, 1), (3, 7)], 3: []}
start_node = 0
end_node = 3

Output: 4 Explanation: The path 0 -> 2 -> 1 -> 3 has a total time of 1 + 1 + 2 = 4.

Edge Cases

  • Start node equals end node (time should be 0).
  • No path exists between nodes.
  • Disconnected graph components.

Hints

Hint 1

Since all edge weights are non-negative, consider using a greedy algorithm to explore the graph.

Hint 2

A priority queue can help efficiently track the node with the currently known minimum travel time.

Hint 3

Maintain a distance array or dictionary initialized to infinity to keep track of the shortest time found to reach each node so far.

Follow-up questions

  • How would your approach change if some road segments had negative weights due to historical traffic patterns (e.g., temporary speed boosts)?
  • How would you modify the function to return the actual path taken in addition to the total travel time?
  • If the graph is massive and cannot fit into memory, what storage strategy would you use to handle the graph data?