Count Distinct Fastest Routes Between Pickup and Dropoff

Last updated: November 29, 2026

Quick Overview

Given an undirected road graph with positive travel times, count how many distinct routes from the pickup node to the dropoff node achieve the minimum total travel time. Return the count modulo 1,000,000,007.

Uber
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given an undirected road graph with positive travel times, count how many distinct routes from the pickup node to the dropoff node achieve the minimum total travel time. Return the count modulo 1,000,000,007.

An Uber technical screen variant of Dijkstra, motivated by offering riders alternative routes with the same ETA. The interviewer checks that you carry a path count alongside the distance and reset it on a strict improvement.

What the Interviewer Expects
  • Extends Dijkstra with a ways array
  • Resets ways on a strictly shorter distance
  • Adds ways on an equal distance
  • Processes each node only after its distance is final
  • Applies the modulus
Key Topics to Cover
Dijkstra
Counting paths
Dynamic programming on DAG
Modular arithmetic
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you count routes within 5 percent of the fastest?
  • Why does this need positive weights, and what breaks with zero-weight edges?
  • How would you sample one fastest route uniformly at random?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: n, roads as (u, v, t) with t > 0, src, dst. Output: number of shortest paths mod 109+710^9+7; 0 if unreachable. Two routes are distinct if their edge sequences differ....

High-Level Design

Shortest paths form a DAG where every edge u -> v satisfies dist[u] + w = dist[v]. Dijkstra visits nodes in increasing distance, which is a topological order of that DAG. So when relaxing edge `u ...


Submit Your Answer
Markdown supported